Android day 5 --> create a custom control, android day 5
1. iPhone-like style. Place a title bar on the top of the interface.
<? Xml version = "1.0" encoding = "UTF-8"?> <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent"> <LinearLayout android: layout_width = "match_parent" android: layout_height = "50dp" android: background = "# 2197db" android: orientation = "horizontal" android: layout_alignParentTop = "true" android: layout_alignParentLeft = "true" android: layout_alignParentStart = "true"> <Button android: id = "@ + id/title_back" android: layout_width = "90dp" android: layout_height = "40dp" android: layout_gravity = "center" android: layout_margin = "5dp" android: text = "back"/> <TextView android: id = "@ + id/title_text" android: layout_width = "0dp" android: layout_height = "wrap_content" android: layout_gravity = "center" android: layout_weight = "1" android: gravity = "center" android: text = "title" android: textColor = "# fff" android: textSize = "24sp"/> <Button android: id = "@ + id/title_edit" android: layout_width = "90dp" android: layout_height = "40dp" android: layout_gravity = "center" android: layout_margin = "5dp" android: text = "OK"/> </LinearLayout> </RelativeLayout>
The title bar layout has been completed, and the rest is how to use this title bar in the program.
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent"> <include layout = "@ layout/title"/> </LinearLayout> // you only need to introduce the title bar layout through a line of include statements.
Then, hide the built-in title bar in MainActivity.
public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_main);}}
We still need to write an event registration code for these controls in each activity. For example, the return button in the title bar has the same functions no matter which activity it is in, that is, the current activity is destroyed. In this case, it is best to use a custom control to solve the problem.
Create a custom title bar control:
public class TitleLayout extends LinearLayout {public TitleLayout(Context context, AttributeSet attrs) {super(context, attrs);LayoutInflater.from(context).inflate(R.layout.title, this);}}
/*
We have rewritten the constructor with two parameters in LinearLayout. When the TitleLayout control is introduced in the layout, this constructor is called. Then, you need to dynamically load the title bar layout in the constructor.
Help LayoutInflater. The from () method of LayoutInflater can be used to construct a LayoutInflater object, and then call the inflate () method to dynamically load a layout file. The inflate () method receives two parameters, the first parameter is the id of the layout file to be loaded. Here we pass in the R. layout. title, the second parameter is to add a parent layout for the loaded layout. Here we want to specify it as TitleLayout, So we directly pass this
*/
Add this custom control to the layout file.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent" ><com.example.xxxxxx.TitleLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"></com.example.xxxxxx.TitleLayout></LinearLayout>
Let's try to register the click event for the button in the title bar and modify the code in TitleLayout.
Public class TitleLayout extends LinearLayout {public TitleLayout (Context context, AttributeSet attrs) {super (context, attrs); LayoutInflater. from (context ). inflate (R. layout. title, this); Button titleBack = (Button) findViewById (R. id. title_back); Button titleEdit = (Button) findViewById (R. id. title_edit); titleBack. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {(Activity) getContext ()). finish () ;}}); titleEdit. setOnClickListener (new OnClickListener () {public static final String TAG = ""; @ Override public void onClick (View v) {Toast. makeText (getContext (), "re-run the program", Toast. LENGTH_SHORT ). show (); Log. I (TAG, "111 ");}});}}