使用9-Patch图片实现不失真按钮背景

来源:互联网 发布:昆明市工信委大数据局 编辑:程序博客网 时间:2024/06/06 04:09

1、使用Draw 9-patch工具制作图片

2、布局文件

<?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:padding="5px"    android:orientation="vertical" > <Button        android:id="@+id/button1"        android:background="@drawable/green1"        android:layout_margin="5px"        android:layout_width="match_parent"        android:layout_height="50px"        android:text="我是普通图片背景"       />      <Button        android:id="@+id/button2"        android:background="@drawable/green"        android:layout_margin="5px"        android:layout_width="450px"        android:layout_height="150px"        android:text="我是9-Patch图片背景(按钮宽度和高度固定)"       />  <Button        android:id="@+id/button3"        android:background="@drawable/button_state"        android:layout_margin="5px"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="我是9-Patch图片背景(单击会变色)"       /> </LinearLayout>


3、res\drawable-mdpi目录下,创建button_state.xmll

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" ><item android:drawable="@drawable/red" android:state_pressed="true"/>    <item android:drawable="@drawable/green" android:state_pressed="false"/></selector>



0 0