Introduction
A statelistdrawable is a drawable object defined in XML that uses a several different images to represent the same graphic , depending on the the object. For example, a Button widget can exist in one of several different states (pressed, focused, or niether) and, using a stat E list drawable, you can provide a different background image for each state.
You can describe the "state" list in an XML file. Each graphic was represented by an "item" element, inside a single "selector" element. Each of the "item" uses various attributes to describe the state which it should is used as the graphic for the drawable.
During Each state change, the state list is traversed top to bottom and the first item this matches the current state is U Sed-the selection is isn't based on the ' best match, ' but simply the first item that meets the minimum criteria of the State .
Translation:
Statelistdrawable is a drawable that is defined by an XML file and uses several different images to render the same graphic, which is toggled by the state of the object. For example, a button has several different states (press, get focus, and so on), in which case you can use a different background image in different states by using the State list drawable.
You can describe the state list in an XML file. Add each graphic by defining an item element under the root node selector. Each item uses a different state attribute to define the unused drawable.
The state list is traversed from top to bottom when each change is made, and the first item that matches the current status will be used-the choice is not to make the "best match" result, but simply to find the first matching state.
Selector is generally used as the background of a state-changed view to achieve a change in the view state when the user is manipulating the view, causing the user to perceive the state of the view as it changes.
Official Notes
File location:res/drawable/filename.xml
Compile resource type:statelistdrawable
Resource reference:
In Java: r.drawable.filename
In XML: @[package:]drawable/filename
Grammar:
<?xml version="1.0"encoding="Utf-8"? ><selector xmlns:android="Http://schemas.android.com/apk/res/android"android:constantsize=["true" | "False"]android:dither=["true" | "False"]android:variablepadding=["true" | "False"] ><item android:drawable="@[package:]drawable/drawable_resource"android:state_pressed=["true" | "False"]android:state_focused=["true" | "False"]android:state_hovered=["true" | "False"]android:state_selected=["true" | "False"]android:state_checkable=["true" | "False"]android:state_checked=["true" | "False"]android:state_enabled=["true" | "False"]android:state_activated=["true" | "False"]android:state_window_focused=["true" | "False"]/></selector>
For more detailed instructions, please refer to Xsoftlab
actual use
Here is a simple example of the button background based on the state to do a bit of processing
XML file
Selector_ts.xml
<?xml version= "1.0" encoding= "Utf-8"?><selector xmlns:android="Http://schemas.android.com/apk/res/android"> <item android:drawable="@color/pink" android:state_pressed="true" /> <item android:drawable="@color/yellow" android:state_selected= "True" /> <item android:drawable="@drawable/shaperect" android:state_enabled ="false" /> <item android:drawable="@color/stone" android:state_enabled="true " /></selector>
Main layout file (activity_main.xml)
<?xml version= "1.0" encoding= "Utf-8"?><LinearLayout xmlns:android="Http://schemas.android.com/apk/res/android" Xmlns:tools="Http://schemas.android.com/tools" Android:layout_width="Match_parent" Android:layout_height="Match_parent" android:focusable="true" Android:focusableintouchmode="true" android:orientation="Vertical" android:padding="10DP" Tools:context="Mraz.com.tabdemo.MainActivity"> <button android:id = "@+id/bt_content" android:layout_width = "match_parent" android:layout_height = "300DP" android:background = "@drawable/selector_ts" /> <linearlayout android: Layout_width = "match_parent" android:layout_ Height = "wrap_content" android:layout_ MarginTop = "50DP" android:orientation = "horizontal" ; <button android:id= "@+id/bt_selected" android:layout_width =< Span class= "Hljs-value" > "wrap_content" android:layout_height = "wrap_content" android:layout_weight =" 1 " android:text =" select " android:textallcaps = "false" /> <button android:id= "@+id/bt_disable" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_weight =" 1 " android:text =" Disable " android:textallcaps = "false" /> <buttonandroid:id="@+id/bt_pressed"android:layout_width="Wrap_ Content "android:layout_height=" Wrap_content "android:layout_weight=" 1 " Android:text="Press"android:textallcaps="false" /> </linearlayout></linearlayout>
The code part is relatively simple, here do not occupy too much space, look at the simple effect, we should know how to write a small activity.
Actual effect
More content awaits the arrival of the excavator ~
<android Basics (13) > Selector