Android 7 (creating custom controls) and android controls

Source: Internet
Author: User

Android 7 (creating custom controls) and android controls

I learned how to use the basic controls and layout of android, but the basic controls and layout sometimes cannot implement complex la S. Let's take a look at the relationship between various controls and la S.



It can be seen that all controls inherit the View directly or indirectly, and all la s are directly or indirectly based on the ViewGroup. View is the most basic UI component in Android. It can draw a rectangular area on the screen and respond to various events in this area. Therefore, the various controls we use are actually added with various unique features on the basis of the View. ViewGroup is a special View that can contain many views and sub-viewgroups. It is a container used to place controls and la S. There are two simple ways to create custom controls: one is to introduce layout and the other is to create custom controls.

Create a project named UICustomViews.

Next we will define a title bar.

Create a layout with the following code:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:orientation="horizontal"    android:background="@drawable/title_bg" >        <Button         android:id="@+id/title_back"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:layout_margin="5dip"        android:background="@drawable/back_bg"        android:text="Back"        android:textColor="#fff"        />    <TextView         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"        android:textColor="#fff"        android:textSize="24sp"        />        <Button         android:id="@+id/title_edit"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:layout_margin="5dip"        android:background="@drawable/edit_bg"        android:text="Edit"        android:textColor="#fff"        />        </LinearLayout>


The code for modifying the main layout is as follows:

<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"    >    <include layout="@layout/title"/></LinearLayout>



Modify the onCreate method in MainActivity

The Code is as follows:

@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_main);}


<Include layout = "@ layout/title"/> is to introduce a layout file.

RequestWindowFeature (Window. FEATURE_NO_TITLE); hides the built-in title bar.

The program running result is as follows:



With the above layout introduction method, no matter how many la s need to be added to the title bar, you only need a line of include statements.


The layout technique solves the problem of repeated code writing. However, if the layout has some controls that can respond to events, we still need to write an event registration code for these controls in each activity. But the registration code is the same, so this will bring a lot of repeated code, then the following is a custom control.


Custom Controls


The new TitleLayout inherits LinearLayout. The code and description are as follows:

Package com. wj. uicustomviews; import android. app. activity; import android. content. context; import android. util. attributeSet; import android. view. layoutInflater; import android. view. view; import android. widget. button; import android. widget. linearLayout; import android. widget. toast; public class TitleLayout extends LinearLayout {public TitleLayout (Context context, AttributeSet attrs) {super (context, attrs );// TODO Auto-generated constructor stub/** rewrite the constructor with two parameters in LinearLayout, when the TitleLayout control is introduced in the layout, the constructor * is called, and then the title bar layout needs to be dynamically loaded in the constructor. This is achieved through 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 parameter numbers, the first parameter is the id of the layout 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 will pass this ***/LayoutInflater. from (context ). inflate (R. layout. title, this); // process the Button event. Button titleBack = (Button) findViewById (R. id. title_back); Button titleEdit = (Button) findViewById (R. id. title_edit); titleBack. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stub (Activity) getContext ()). finish () ;}}); titleEdit. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubToast. makeText (getContext (), "you clicked edit button", Toast. LENGTH_SHORT ). show ();}});}}


Reference the custom control in the layout file to modify the main layout file. The Code is as follows:

<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"    >    <!-- <include layout="@layout/title"/> -->    <com.wj.uicustomviews.TitleLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        >            </com.wj.uicustomviews.TitleLayout></LinearLayout>


The running result is as follows:




Click the button to respond to the event.

Note: When adding a custom control, you must specify the complete package name and class name.


Reprinted Please note: http://blog.csdn.net/j903829182/article/details/40682583




Question about Android custom controls?

Inheriting a View is mainly to define a control. For example, if the buttons that come with Android are inappropriate, you can inherit the view and implement them by yourself.
Inheriting ViewGroup or LinaryLayout is to customize a layout and inherit ViewGroup to implement more functions.

Question about android custom control development ???

You can set the id for RelativeLayout, and then set onclickListener in the java file. TextView can also do the same.
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.