Button是android视图的基本组件,有以下常用方法:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="图片切换"
android:background="@drawable/btnselector"/> //引用btnselector.xml文件,如下
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:text="图片切换"
android:background="@drawable/btnselector"/>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="原图片"
android:background="@drawable/btn_small"/>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="处理后的图片"
android:background="@drawable/btn_small_cls"/> //用draw9patch处理过的图片,在拉升时圆角不会改变
<Button
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="图片"
android:drawableTop="@drawable/ic_launcher" //设置图片的位置,可以实现图片与文字共存
android:background="@drawable/btn_small_cls"/>
</LinearLayout>
btnselector.xml文件,自己新建,位置于res/drawable-mdpi下,用于切换图片等资源
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/btn_small_null" android:state_enabled="false"/>
<item android:drawable="@drawable/btn_small" android:state_pressed="false"/>
<item android:drawable="@drawable/btn_small_hover" android:state_pressed="true"/>
</selector>