Patchwork custom control

Patchwork custom control

Reference layout technique does solve the problem of rewriting the layout code, but there is room for optimization.
For example, the back button, in fact, no matter which event, the button functions are identical title bar, which destroyed current Activity.
This situation is the best way to solve using custom controls.

1 New TitleLayout inherited from LinearLayout, let it be the title bar control our custom

Code as follows:

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  extends LinearLayout {

    public (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() {
            
            public void onClick(View v) {
                
                ((Activity) getContext()).finish();
            }
        });

        titleEdit.setOnClickListener(new OnClickListener() {
            
            public void onClick(View v) {
                // 吐司
                Toast.makeText(getContext(), "你点击了右侧按钮", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

tiele layout file

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/bg_banner" >

    <Button
        android:id="@+id/title_back"
        an 大专栏  拼凑自定义控件droid:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="5dip"
        android:background="@drawable/bt_menu_show"
        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/ico_letter"
        android:text="Edit"
        android:textColor="#fff" />

</LinearLayout>

2 Add the custom control in the layout of Activity

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.yassblog.TitleLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </com.yassblog.TitleLayout>

</LinearLayout>   

3 In this case, every time we introduce the layout of a page of TitleLayout, the left and right button click event returns to the Edit button has been automatically good, but also save a lot of work to write duplicate code.

4 If a page, two buttons have different functions, by reference layout implementations

Guess you like

Origin www.cnblogs.com/lijianming180/p/12147462.html