Android之DIYViews(自定義控件)

引入佈局

問題:我們的程序中可能有很多活動都需要標題欄,如果在每個活動的佈局中都編寫一遍同樣的標題欄代碼,明顯會導致代碼的大量重複

解決方案:引入佈局

一、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="#ff0000">

    <Button
        android:id="@+id/title_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:text="Back"
        android:textColor="#fff"
        />

    <TextView
        android:id="@+id/title_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="center"
        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_margin="5dp"
        android:text="Edit"
        android:textColor="#fff"
        />

</LinearLayout>

二、如何在程序中使用該標題欄:activity_main.xml

通過一個include語句引入進來即可

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <!-- 將標題欄佈局引入進來   -->
    <include
        layout="@layout/title"
        />


</LinearLayout>

 


創建自定義控件

引入佈局的技巧解決了重複編寫佈局代碼的問題。

問題:但是如果佈局中某些控件要求能夠響應外部事件,需要在每個活動中單獨編寫一次事件編寫的代碼。比如說:標題欄的返回按鈕---->不管在哪個活動中,這個按鈕的功能都相同,即銷燬當前活動。而如果在每個活動中都需要重新註冊一遍返回按鈕的點擊事件,無疑會增加很多重複代碼

解決方案:自定義控件

一、TitleLayout,繼承自LinearLayout,自定義的標題欄控件

package com.example.diyviews;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

public class TitleLayout extends LinearLayout {


    public TitleLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        LayoutInflater.from(context).inflate(R.layout.title,this);

    }
}

二、activity_main.xml:在佈局文件中添加上面的自定義控件

添加自定義控件個普通該控件的方式基本一樣,只不過添加自定義控件,需要指明控件的完整類名

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

    <!-- 添加自定義控件,需要指明控件的完整類名   -->
    <com.example.diyviews.TitleLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
    />

</LinearLayout>

三、修改TitleLayout代碼:爲標題欄的按鈕註冊點擊事件

package com.example.diyviews;

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;

import androidx.annotation.Nullable;

public class TitleLayout extends LinearLayout {


    public TitleLayout(Context context, @Nullable 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() {
            @Override
            public void onClick(View v) {
                ((Activity)getContext()).finish();
            }
        });

        titleEdit.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getContext(),"You clicked Edit Button",Toast.LENGTH_SHORT).show();
            }
        });

    }
}

疑問:

((Activity)getContext()).finish();
Toast.makeText(getContext(),"You clicked Edit Button",Toast.LENGTH_SHORT).show();

答疑: 

getContext():這個是View類中提供的方法,在繼承了View的類中纔可以調用。返回的是當前View運行在哪個Activity Contex中(獲取當前context的實例)

四、MainActivity.java

package com.example.diyviews;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.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);

        //將系統自帶的標題欄隱藏掉
        ActionBar actionBar=getSupportActionBar();//獲取ActionBar實例
        if(actionBar!=null){
            actionBar.hide();
        }

    }
}
getSupportActionBar() 獲取當前ActionBar的實例
hide() 將標題欄隱藏起來

 

五、效果圖

 

 

 

 

 

 

 

 

 

發佈了64 篇原創文章 · 獲贊 5 · 訪問量 9324
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章