Analysis of Example Code of Picture and Button Press State Change in Android
- 2021-09-12 02:00:52
- OfStack
1. Set the background selector for pictures, so as to click or set whether it is selected or not, and switch the background
res/drawable/selector_settings_item_back.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="@color/settingsSelectedItem"/>
<item android:state_pressed="true" android:drawable="@color/settingsSelectedItem"/>
<item android:state_selected="true" android:drawable="@color/settingsSelectedItem"/>
<item android:state_focused="false" android:drawable="@color/settingsItem"/>
</selector >
Color value definition:
res/values/colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="settingsItem">#ffffff</color>
<color name="settingsSelectedItem">#FFA500</color>
</resources>
2. Round corner button, press and lift to switch the background and switch the text color at the same time
res/layout/activity_xxx.xml
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/selector_shape_corner_button"
android:text=" Under approval " android:textColor="@drawable/selector_font_style_corner_button"
android:textSize="13sp"
/>
Which refers to two selector under res/drawable/,
One is to switch the background picture with clicking and lifting state, and the other is to switch the text color with clicking and lifting.
res/drawable/selector_shape_corner_button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="@drawable/shape_corner_button_fill"/>
<item android:state_pressed="true" android:drawable="@drawable/shape_corner_button_fill"/>
<item android:state_selected="true" android:drawable="@drawable/shape_corner_button_fill"/>
<item android:state_focused="false" android:drawable="@drawable/shape_corner_button"/>
</selector >
res/drawable/selector_font_style_corner_button
shape_corner_button.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="5dp" />
<solid
android:color="#001da1f2" />
<stroke
android:width="1dp"
android:color="#1da1f2" />
</shape>
shape_corner_button_fill.xml
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="5dp" />
<solid
android:color="#ff1da1f2" />
<stroke
android:width="1dp"
android:color="#1da1f2" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:color="#ffffff"/>
<item android:state_pressed="true" android:color="#ffffff"/>
<item android:state_selected="true" android:color="#ffffff"/>
<item android:state_focused="false" android:color="#1da1f2"/>
</selector >
Summarize