This series is the "first line of Android Code" Learning notes, if there are errors and omissions, please correct me!
View is one of the most basic UI components on Android that can draw a rectangular area on the screen and respond to various events in this area, so the various controls we use actually add their own functionality to the view base. ViewGroup is a special view that can contain many sub-view and sub-viewgroup, and is a container for placing controls and layouts. All of the system's default controls are inherited directly or indirectly from View, and all of the layouts used are directly or indirectly inherited from ViewGroup. If the system comes with a control that does not meet our needs, we can create a custom control.
One, custom layout file
1) Build the layout file:
Let's create a new layout file, add some of the required controls, and the code is as follows:
1 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Android:layout_width= "Match_parent"3 Android:layout_height= "Wrap_content"4 Android:background= "@drawable/title_bg" >5 <Button6 Android:id= "@+id/title_back"7 Android:layout_width= "Wrap_content"8 Android:layout_height= "Wrap_content"9 android:layout_gravity= "Center"Ten Android:layout_margin= "5dip" One Android:background= "@drawable/back_bg" A Android:text= "Back" - Android:textcolor= "#fff" /> - <TextView the Android:id= "@+id/title_text" - Android:layout_width= "0dip" - Android:layout_height= "Wrap_content" - android:layout_gravity= "Center" + Android:layout_weight= "1" - android:gravity= "Center" + Android:text= "Title Text" A Android:textcolor= "#fff" at android:textsize= "24SP" /> - <Button - Android:id= "@+id/title_edit" - Android:layout_width= "Wrap_content" - Android:layout_height= "Wrap_content" - android:layout_gravity= "Center" in Android:layout_margin= "5dip" - Android:background= "@drawable/edit_bg" to Android:text= "Edit" + Android:textcolor= "#fff" /> - </LinearLayout>
We defined two buttons and a textview in the layout file, where the android:background is the background of the control or layout in the picture file passed into the Res. Android:layout_margin This property, you can specify how far the control will be offset in the upper and lower left and right directions, or you can use Android:layout_marginleft or android:layout_margintop Properties to specify the distance that the control is offset in a direction.
2) Introduce the layout file:
When we want to introduce a custom layout file into our activities, we just need to add a line of code:
1 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Android:layout_width= "Match_parent"3 Android:layout_height= "Match_parent" >4 <includeLayout= "@layout/title" />5 </linearlayout>
We have added the <include layout= "@layout/title"/> to the layout file, and you can let the activity load this layout, but remember to hide the title bar from the system when it is active.
two, custom control :
Many activities will have the same control, such as the back key, if we write the event registration code for each of them in each activity, this will undoubtedly increase the amount of code. To do this, we can use a custom control to resolve this problem.
1) Create a new control class:
1 public class titlelayout extends LinearLayout { 2 public Titlelayout (context context, AttributeSet attrs) { 3 super (context, attrs); 4 layoutinflater.from (context). Inflate (R.layout.title, this ); 5 6 }
In the code we rewrite the constructor with two parameters in LinearLayout, which is called when the Titlelayout control is introduced into the layout. The title bar layout needs to be dynamically loaded in the constructor, which is accomplished with the help of Layoutinflater. The Layoutinflater from () method allows you to build a Layoutinflater object and then call the inflate () method to dynamically load a layout file, and the inflate () method receives two parameters. The first parameter is the ID of the layout file to be loaded, here we pass in R.layout.title, the second parameter is to add a parent layout to the loaded layout, where we want to specify as Titlelayout, and then pass in this directly.
2) Add this control to the layout file:
1 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Android:layout_width= "Match_parent"3 Android:layout_height= "Match_parent" >4 <Com.example.uicustomviews.TitleLayout5 Android:layout_width= "Match_parent"6 Android:layout_height= "Wrap_content"7 ></Com.example.uicustomviews.TitleLayout>8 </LinearLayout>
The way to add custom controls and add normal controls is basically the same, except that when you add a custom control, we need to indicate the full class name of the control, and the package name cannot be omitted here. At this point, we can call these controls in the activity as if they were normal controls.
End.
Android Learning Notes (ix)--customization of layouts and controls