安卓topbar编码实战

1.先在res->value下新建attrs.xml文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="topBar">
        <attr name="title" format="string"/>
        <attr name="titleTextSize" format="dimension"/>
        <attr name="titleTextColor" format="color"/>
    </declare-styleable>
</resources>

2.再编写组合布局,在layout下新建一个topbar.xml,左中右

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/topbar_root"

    android:layout_width="match_parent"
    android:layout_height="45dp">

    <Button
        android:text="left"
        android:layout_alignParentLeft="true"
        android:layout_centerInParent="true"
        android:id="@+id/topbar_leftbtn"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />

    <TextView
        android:id="@+id/topbar_tv"
        android:textStyle="bold"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/topbar_rightbtn"
        android:text="right"
        android:layout_alignParentRight="true"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />
</RelativeLayout>

3.编写布局的加载类

package com.lingdangmao.demo_zidingyi_textview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * Created by Administrator on 2018/1/8.
 */

public class Topbar extends RelativeLayout {

    private Button topbarLeftBtn,topbarRightBtn;
    private TextView topbarTextView;
    private RelativeLayout topbar_root;
    private int mColor= Color.BLUE;
    private int mTextColor=Color.WHITE;
    private String title;


    public Topbar(Context context, AttributeSet attrs) {
        super(context, attrs);
        //获得从外面加载的数据
        initTypedArray(context,attrs);
        //初始化页面
        initView(context);
    }
    private void initTypedArray(Context context, AttributeSet attrs){
        TypedArray ta =context.obtainStyledAttributes(attrs,R.styleable.topBar);
        mTextColor = ta.getColor(R.styleable.topBar_titleTextColor,Color.WHITE);
        title = ta.getString(R.styleable.topBar_title);
        ta.recycle();
    }
    private void initView(Context context){
        LayoutInflater.from(context).inflate(R.layout.topbar,this,true);
        topbar_root =findViewById(R.id.topbar_root);
        topbarLeftBtn = findViewById(R.id.topbar_leftbtn);
        topbarRightBtn =findViewById(R.id.topbar_rightbtn);
        topbarTextView =findViewById(R.id.topbar_tv);

        //设置背景颜色
        topbar_root.setBackgroundColor(mColor);
        //设置文字颜色
        topbarTextView.setTextColor(mTextColor);

        //设置文字标题
        setTitle(title);
        //绑定事件
        topbarLeftBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("ww", "onClick111: ");
                listener.OnleftBtnClick();
            }
        });
    }

    private void setTitle(String title){
        if(!title.isEmpty()){
            topbarTextView.setText(title);
        }
    }
    private topbarOnClickListener listener;

    public interface topbarOnClickListener{
        void OnleftBtnClick();
        void OnRightBtnClick();
    }
    public void setTopbarClickListener(topbarOnClickListener listener){
        this.listener=listener;
    }

    public void setLeftOnClickListener(OnClickListener onClickListener){
        topbarLeftBtn.setOnClickListener(onClickListener);
    }

}

4.在mainactivity中使用

package com.lingdangmao.demo_zidingyi_textview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {

    private Topbar topbar;
    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        topbar  =findViewById(R.id.title);

        topbar.setTopbarClickListener(new Topbar.topbarOnClickListener() {
            @Override
            public void OnleftBtnClick() {
                Log.d(TAG, "OnleftBtnClick: ");
            }
            @Override
            public void OnRightBtnClick() {

            }
        });

    }
}

5.在主要布局中加载组合布局

<?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"
    android:id="@+id/main_root_ll"
    tools:context="com.lingdangmao.demo_zidingyi_textview.MainActivity">

    <com.lingdangmao.demo_zidingyi_textview.Topbar
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/title"
        app:title="自定义组合控件"
        app:titleTextSize="20dp"
        app:titleTextColor="#ff0038"
        android:layout_width="match_parent"
        android:layout_height="45dp">

    </com.lingdangmao.demo_zidingyi_textview.Topbar>


</LinearLayout>

 最后完成的效果下图

原文地址:https://www.cnblogs.com/norm/p/8244882.html