Android使用xml定义带背景Button(解决ImageButton不能加文字的问题)
2012-09-04 21:16
483 查看
虽然Android给我们提供了ImageButton 这一组件来实现带有自定义图片背景的Button,但是ImageButton有一个缺陷就是不能添加文字而如下的方法则可以综合二者的优势.
用到的图片:
在drable下新建一个button_blue_style.xml文件来描述Button的按下效果:
然后按照使用普通Button的方法在布局文件中加入该Button
效果图如下:
用到的图片:
在drable下新建一个button_blue_style.xml文件来描述Button的按下效果:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/button_blue_click" /> <!-- pressed --> <item android:state_focused="true" android:drawable="@drawable/button_blue_click" /> <!-- focused --> <item android:drawable="@drawable/button_blue_nomal" /> <!-- default --> </selector>
然后按照使用普通Button的方法在布局文件中加入该Button
<Button android:id="@+id/renrenButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/textView2" android:layout_centerHorizontal="true" android:background="@drawable/button_blue_style" android:drawableLeft="@drawable/renrenicon" android:text="@string/login_usingrenren" android:textColor="#000000" />需要说明的是为了使Button能够适应不同长度文字内容,我们使用.9PNG作为Button背景.
效果图如下:
相关文章推荐
- Android 将Activity打成jar包 供第三方使用 彻底解决XML 图片 文字资源不可打入到jar包的问题!
- android使用xml定义背景边框
- 使用ScrollView属性fillViewport解决android布局不能撑满全屏的问题
- 解决android自带textview右侧文字不能对齐的问题
- 解决导入android工程后main.xml Graphical Layout不能正常显示的问题
- Android button, xml文件定义形状,代码中修改背景颜色
- 使用ScrollView属性fillViewport解决android布局不能撑满全屏的问题
- [Android] Android Studio 解决Button按钮文字全大写的问题
- Android中ListView点击和ListView的item里面的Button或ImageView不能同时生效问题的解决
- Android GridView 的item 使用 Button,imagebutton 等ui控件后,不能响应OnItemClick()事件
- android使用.9图作为背景,内容不能居中的问题解决方案
- 解决firefox下button文字不能垂直居中的问题
- Android中Button文字不能一行显示解决方法
- 使用ScrollView时解决android布局不能撑满全屏的问题
- Android源码编译之后G-Sensor不能使用问题的解决
- android:drawableLeft的Button不能切换图片问题之解决办法
- android定义新的api解决一些低版本不能运行的问题
- 使用ScrollView属性fillViewport解决android布局不能撑满全屏的问题
- Android 将Activity殴打jar包 对于由第三方使用 解决XML 图片 文本资源并不难过进入jar包装问题!
- android定义新的api解决一些低版本不能运行的问题