《第一行代碼》筆記1-創(chuàng)建自定義控件

新建一個工程,實現(xiàn)一個類iOS系統(tǒng)導(dǎo)航條樣式的菜單
1.在res layout目錄下新建一個title.xml文件,如下:

<?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:background="#8F6D6D">

    <Button
        android:id="@+id/title_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        android:background="#59B08F"
        android:text="Back"
        android:textColor="#ffffff"/>

    <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 Text"
        android:textColor="#ffffff"
        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="5dp"
        android:background="#CB2B2B"
        android:text="edit"
        android:textColor="#ffffff"/>
</LinearLayout>
  1. 直接在activity_main.xml布局文件中通過include的方式把title布局引入:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <include layout="@layout/title" />
</LinearLayout>
  1. 將系統(tǒng)自帶的菜單隱藏掉:
package com.example.customtitle;

import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 隱藏掉系統(tǒng)自帶的標題欄
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }

    }
}

運行:

Screenshot_2017-09-17-10-24-48-657_CustomTitle.png

這樣就實現(xiàn)了一個菜單欄,只需要在需要的布局中引入即可。
但是這種include的方式有一個缺陷,一般菜單欄都是要響應(yīng)一些事件的,比如返回按鈕負責(zé)返回上一個activity等等。如果在每個引入的activity里都單獨去實現(xiàn)這些方法會比較冗余。

這種時候就需要自定義一個控件了。
新建一個TitleLayout類繼承自LinearLayout,并在該布局中加載title.xml資源,并定義相關(guān)的事件即可.

package com.example.customtitle;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

/**
 * Created by archerlj on 2017/9/17.
 */

public class TitleLayout extends LinearLayout {

    public TitleLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        // this 表示R.layout.title的父布局,就是TitleLayout
        LayoutInflater.from(context).inflate(R.layout.title, this);

        // 返回按鈕事件
        findViewById(R.id.title_back).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                ((Activity)getContext()).finish();
            }
        });

        // 編輯按鈕事件,這里彈出一個toast即可。
        findViewById(R.id.title_edit).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getContext(), "Eidt clicked", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

2.定義好layout之后,只需要在需要的activity布局中像普通控件一樣使用即可。打開activity_main.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <!--<include layout="@layout/title" />-->

    <com.example.customtitle.TitleLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></com.example.customtitle.TitleLayout>
</LinearLayout>

運行:
點擊back按鈕即可退出程序,點擊edit按鈕可彈出一個toast。

這樣每當(dāng)我們需要一個菜單欄的時候,直接在布局文件中引入即可,并且菜單中的事件也會自動實現(xiàn)了。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容