转载请标明出处:https://blog.youkuaiyun.com/hj_key/article/details/89737746
整理下button 按下效果,切换背景:
在drawable下新建bg_hangup.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="false" android:drawable="@drawable/haangup" /> <item android:state_focused="true" android:drawable="@drawable/number_press_dial" /> <item android:state_pressed="true" android:drawable="@drawable/number_press_dial" /> </selector>
使用时直接 在view下添加
android:src="@drawable/haangup"
PS:附上拨号自定义盘布局改写
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/all_screen" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!--android:layout_width="368px"--> <!--android:layout_height="69px"--> <LinearLayout android:id="@+id/txt_number_line" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:visibility="visible"> <LinearLayout android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/line_color" /> <!--android:textSize="40sp"--> <!--android:textSize="44px"--> <EditText android:id="@+id/numberStr" android:layout_width="match_parent" android:layout_height="115dp" android:background="#000000" android:gravity="right|center" android:paddingLeft="17dip" android:paddingRight="20dp" android:text="123 4567 890" android:textColor="@color/color_white" android:textSize="40sp" android:textStyle="bold" /> <LinearLayout android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/line_color" /> </LinearLayout> <!--android:layout_width="368px"--> <!--android:layout_height="320px"--> <LinearLayout android:id="@+id/table_num" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/txt_number_line" android:layout_weight="6" android:background="@color/line_color" android:orientation="vertical"> <LinearLayout style="@style/layout1_input_amount_style"> <Button android:id="@+id/one" style="@style/btn_input_amount_style" android:layout_marginRight="1dp" android:background="@drawable/bg_number_1" /> <Button android:id="@+id/two" style="@style/btn_input_amount_style" android:layout_marginRight="1dp" android:background="@drawable/bg_number_2" /> <Button android:id="@+id/three" style="@style/btn_input_amount_style" android:background="@drawable/bg_number_3" /> </LinearLayout> <LinearLayout style="@style/layout_input_amount_style"> <Button android:id="@+id/four" style="@style/btn_input_amount_style" android:layout_marginRight="1dp" android:background="@drawable/bg_number_4" /> <Button android:id="@+id/five" style="@style/btn_input_amount_style" android:layout_marginRight="1dp" android:background="@drawable/bg_number_5" /> <Button android:id="@+id/six" style="@style/btn_input_amount_style" android:background="@drawable/bg_number_6" /> </LinearLayout> <LinearLayout style="@style/layout_input_amount_style"> <Button android:id="@+id/seven" style="@style/btn_input_amount_style" android:layout_marginRight="1dp" android:background="@drawable/bg_number_7" /> <Button android:id="@+id/eight" style="@style/btn_input_amount_style" android:layout_marginRight="1dp" android:background="@drawable/bg_number_8" /> <Button android:id="@+id/nine" style="@style/btn_input_amount_style" android:background="@drawable/bg_number_9" /> </LinearLayout> <LinearLayout style="@style/layout_input_amount_style"> <Button android:id="@+id/dialing" style="@style/btn_input_amount_style" android:layout_marginRight="1dp" android:background="@drawable/bg_number_dial" /> <Button android:id="@+id/zero" style="@style/btn_input_amount_style" android:layout_marginRight="1dp" android:background="@drawable/bg_number_0" /> <Button android:id="@+id/delete" style="@style/btn_input_amount_style" android:layout_marginBottom="15dp" android:background="@drawable/bg_number_del" /> </LinearLayout> </LinearLayout> </RelativeLayout>
在style 中自定义样式:
<style name="layout_input_amount_style"> <item name="android:layout_width">match_parent</item> <item name="android:layout_height">0dp</item> <item name="android:layout_weight">1</item> <item name="android:layout_marginTop">1dp</item> <item name="android:gravity">center</item> <item name="android:background">@color/line_color</item> <item name="android:orientation">horizontal</item> </style> <style name="layout1_input_amount_style"> <item name="android:layout_width">match_parent</item> <item name="android:layout_height">0dp</item> <item name="android:layout_weight">1</item> <item name="android:gravity">center</item> <item name="android:background">@color/line_color</item> <item name="android:orientation">horizontal</item> </style> <style name="btn_input_amount_style"> <item name="android:layout_width">0dp</item> <item name="android:layout_height">match_parent</item> <item name="android:layout_weight">1</item> <item name="android:textSize">40sp</item> <item name="android:textColor">#ffffff</item> </style>