Today, we take Button and TextView as examples to see how to set the background of View subclass of live broadcast software source code.
Button button
The button can respond to the user's click behavior. Place a button in the xml file.
<Button android:id="@+id/btn" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/self_destruct" />
To listen to button click events, set it in Activity.
public class MyActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.content_layout_id); final Button button = findViewById(R.id.button_id); button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Here is the button click event, which is executed in the main thread } }); } }
We used View.OnClickListener above. After the button is clicked, the onClick method is executed. The live broadcast software source code will execute the onClick method in the main thread of the App. We can update the UI here. But don't do too time-consuming operations.
We notice that OnClickListener is actually an interface in View. setOnClickListener is also a method of View. In other words, even if we don't use button s here, we can listen to click events in this way. That is, View.setOnClickListener(View.OnClickListener()).
In the future, you will encounter TextView, ImageView to listen to click events, or the whole Layout to listen to click events. The live broadcast software source code here uses the listener mode.
In fact, the Button inherits from TextView.
Button, TextView background settings
How to add motion to the button?
Button s can be pressed or not pressed. We can give these two states different background colors and text colors. This article introduces the selector, that is, the status list. Like the previous shape, the selector is also an xml file.
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> </selector>
Set Button background
- Preparing shape files
Prepare the shape file first. Three shapes are prepared here. Each represents three different states.
shape_btn_1_normal.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#6CB3DD" /> <corners android:radius="2dp" /> </shape>
shape_btn_1_pressed.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#1E6283" /> <corners android:radius="4dp" /> </shape>
shape_btn_disable.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#6D6D6D" /> </shape>
- New selector file
New drawable file bg_1.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/shape_btn_disable" android:state_enabled="false" /> <item android:drawable="@drawable/shape_btn_1_normal" android:state_enabled="true" android:state_pressed="false" /> <item android:drawable="@drawable/shape_btn_1_pressed" android:state_enabled="true" android:state_pressed="true" /> </selector>
- Set Button background
Set the background in layout.
<Button android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/bg_1" android:text="RFDev btn 1" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="12dp" android:background="@drawable/bg_1" android:enabled="false" android:text="RFDev btn 2" />
Run it to see the effect. When the button is pressed and not pressed, the background color of the live broadcast software source code button is different.
Introduction to selector
StateListDrawable is a drawable object defined in an XML file. It uses multiple different images to represent the same graph according to the object state.
For example, a Button widget can be one of a variety of different states (pressed, focused, or neither), and an object can be drawn using a state list to provide different background pictures for each state. A list of states can be described in an xml file. Each drawing consists of a single < selector> In element < item> Element representation. each < item> Various attributes are used to describe the state of graphics applied as paintable objects. During each state change, the list of States is traversed from top to bottom and the first item that matches the current state is used - this selection is not based on the best match, but the first item that meets the lowest condition of the state. The selector points to StateListDrawable. Here we don't focus on StateListDrawable, only xml.
grammar
Refer to the following syntax:
<?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>
Precautions for use
We can set the background for the Button or the TextView.
<TextView android:id="@+id/tv1" android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginTop="12dp" android:background="@drawable/bg_1" android:gravity="center" android:text="RFDev text view 1" />
bg_ State is set in 1_ Pressed. If no click event is set for TextView, the background will not change when users click or press this TextView. After setting a click event for TextView, click again to see the background change.
In this article, we use Button and TextView as examples. In fact, other subclasses of View, such as ImageView, LinearLayout and live broadcast software source code, can set the background in this way.
Statement: This article is forwarded by cloudleopard technology from Android_ Anzi blog, if there is infringement, please contact the author to delete