“This is the fifth day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”
👉 About the author
As we all know, life is a long process of constantly overcoming difficulties and reflecting on progress. In this process, there will be a lot of questions and thoughts about life, so I decided to share my thoughts, experiences and stories to find resonance!!
Focus on Android/Unity and various game development tips, as well as various resource sharing (websites, tools, materials, source code, games, etc.)
Welcome to pay attention to the public account [Mr. Empty name] for more resources and communication!
👉 premise
This is small empty stay up late to write Android novice series, welcome to taste.
Selector is the most commonly used method for buttons, and it’s very useful for beautifying controls.
In the last video we talked about using selectors and shapes together, but we’re going to focus on shapes, and we’re going to focus on selectors.
👉 Practice
Let’s start by creating two Shape backgrounds btn_selector_shape1.xml as we did in the previous section
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<! - the rounded - >
<corners android:radius="5dp" />
<! -- Fill color -->
<solid android:color="#00ff00" />
</shape>
Copy the code
btn_selector_shape2.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<! - the rounded - >
<corners android:radius="5dp" />
<! -- Fill color -->
<solid android:color="#0000ff" />
</shape>
Copy the code
And then we’ll right-click on res-drawable and create a drawable Resource File, and it’ll pop up and create the File, and set the default Root element to selector.
btn_selector0.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/btn_selector_shape1" android:state_pressed="true" />
<item android:drawable="@drawable/btn_selector_shape2" android:state_window_focused="false" />
</selector>
Copy the code
Reference in layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context=".TextActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="160dp"
android:background="@drawable/btn_selector0"
android:text="Press color change"
android:textColor="@color/white" />
</RelativeLayout>
Copy the code
So let’s run it
but
So let’s remember, did we just create three files, and it’s fine with a few buttons, but we have a lot of custom buttons, and it’s really hard to manage so many files, so we’re going to write it a different way, and we’re going to put everything in one file.
We just changed it in btn.selector0.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<! -- This is the first way, using drwable to reference files -->
<! --<item android:drawable="@drawable/btn_selector_shape1" android:state_pressed="true" />-->
<! --<item android:drawable="@drawable/btn_selector_shape2" android:state_pressed="false" />-->
<! The second way is as follows -->
<item android:state_pressed="false">
<shape android:shape="rectangle">
<! - the rounded - >
<corners android:radius="5dp" />
<! -- Fill with white -->
<solid android:color="#0000ff" />
</shape>
</item>
<! -- A rectangle with rounded corners, white background and green border when clicked -->
<item android:state_pressed="true">
<shape android:shape="rectangle">
<! - the rounded - >
<corners android:radius="5dp" />
<! -- Fill with white -->
<solid android:color="#00ff00" />
</shape>
</item>
</selector>
Copy the code
Let’s run it and see, hey, it works
There are more than two Selector properties:
- State_focused Boolean value, whether focus is obtained
- State_window_focused Boolean value, whether the window focus is obtained
- State_enabled Specifies whether the control is available
- State_checkable Specifies a Boolean value for whether the control can be checked
- State_checked Boolean value for whether the control is checked
- State_selected Boolean value for whether the control is selected, if there are rollers
- State_pressed Boolean value for whether the control is pressed
- State_active Boolean value for whether the control is active
- State_single and state_first and state_middle are rarely used, so just know.
👉 other
📢 author: Kom and Kom in Kom
📢 reprint instructions – be sure to specify the source: Zhim Granular’s personal home page – column – Nuggets (juejin. Cn)
📢 welcome to like 👍 collect 🌟 message 📝