パッチワークのカスタムコントロール

パッチワークのカスタムコントロール

参考レイアウト技術は、レイアウトのコードを書き換えるの問題を解決しますが、最適化の余地がある。
例えば、ボタンの機能が破壊され、同一のタイトルバー、あるイベント、戻るボタン、実際には、関係なく現在の活動。
この状況は、カスタムコントロールを使用して解決するための最良の方法です。

LinearLayoutから継承された1新TitleLayout、それがタイトルバーには、当社のカスタムを制御することましょう

コードは次の通り:

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レイアウトファイル

<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活動のレイアウトにカスタムコントロールを追加します。

<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に示すように、我々はTitleLayoutのページのレイアウトを導入するたびに、[編集]ボタンの左と右ボタンクリックイベントが戻るだけでなく、重複したコードを書くために多くの作業の保存、自動的に良いされています。

4ページの場合、2つのボタンにより、異なる機能を有する基準レイアウト実装

おすすめ

転載: www.cnblogs.com/lijianming180/p/12147462.html