Android使用NinePatch图片实现大小可变的Button
2012-05-30 11:37
495 查看
在Android的一些应用程序中,有时要用到大小可以延展的图片做背景,实现的方法是使用NinePatch。
下面是一个用NinePatch图片给Button做背景的例子,实现一个可以随文字大小而改变的图片Button:
准备一张NinePatch资源图片(button.9.png),具体方法参考(/article/8675459.html);
将button.9.png拖曳(drag)到android工程的/res/drawable-mdpi目录下。
修改main.XML文件:
这里的做法是,在UI上摆放Button元件,并设定Button上的文字及大小。通过「android:background」属性设定,将Button的背景设定为「@drawable/button」,即「drawable资源(drawable-mdpi/目录)里的button图片」,Android框架会去找到button.9.png档案。因为button.9.png是一张NinePatch图片,因此会随着Button上的文字大小延展。
此时所有工作已完成,不需要改写任何代码,程序运行效果如下:
转载请注明出处:/article/8675460.html
下面是一个用NinePatch图片给Button做背景的例子,实现一个可以随文字大小而改变的图片Button:
准备一张NinePatch资源图片(button.9.png),具体方法参考(/article/8675459.html);
将button.9.png拖曳(drag)到android工程的/res/drawable-mdpi目录下。
修改main.XML文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/button" android:text="music" android:textSize="12sp" /> <Button android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/button" android:text="dialer" android:textSize="24sp" /> <Button android:id="@+id/btn3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/button" android:text="wallpaper" android:textSize="48sp" /> </LinearLayout>
这里的做法是,在UI上摆放Button元件,并设定Button上的文字及大小。通过「android:background」属性设定,将Button的背景设定为「@drawable/button」,即「drawable资源(drawable-mdpi/目录)里的button图片」,Android框架会去找到button.9.png档案。因为button.9.png是一张NinePatch图片,因此会随着Button上的文字大小延展。
此时所有工作已完成,不需要改写任何代码,程序运行效果如下:
转载请注明出处:/article/8675460.html
相关文章推荐
- android之ImageButton实现选中改变背景,附加(图片自动适配组件大小)
- android之ImageButton实现选中改变背景,外加(图片自动适配组件大小)
- Android中使用ClipDrawable实现缓缓打开一张图片的效果
- Android 使用ViewPager实现左右循环滑动图片
- ANDROID中使用开源框架ANDROID-IMAGE-INDICATOR实现图片轮播部署
- Android实现自定义带文字和图片的Button
- Android 使用开源库StickyGridHeaders来实现带sections和headers的GridView显示本地图片效果
- Android中使用HttpURLConnection实现GET POST JSON数据与下载图片
- android viewflipper的使用 实现图片滑动效果
- Android 使用ViewPager实现左右循环滑动图片
- Android 使用ViewPager实现左右循环滑动图片
- Android中ConvenientBanner的使用--获取网络图片 --(实现效果是自动轮播图片)
- Android广告条效果--使用Volley实现网络图片的自动轮播(仿淘宝、网易广告页轮播效果)
- Android使用加载器(Loader)实现获取本机中所有图片
- Android中使用RadioButton实现QQ底部图片切换
- Android编程实现给Button添加图片和文字的方法
- Android 使用ViewPager实现左右循环滑动图片
- Android自定义progressDialog使用系统ProgressBar与图片实现(二)使用图片
- JS限制上传图片大小不使用控件在本地实现
- Android 使用BaseAdapter实现异步图片加载