自定义view标题栏

attrs.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="TopBar">
        <attr name="leftButttonBackgrount" format="reference"/>
        <attr name="rightButttonBackgrount" format="reference"/>
        <attr name="titleText" format="string"/>
        <attr name="titleTextSize" format="dimension"/>
        <attr name="titleTextColor" format="color"/>
    </declare-styleable>
</resources>

创建一个布局topbar.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:background="#4B2E1BD8">
    <Button
        android:id="@+id/leftButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"/>
    <EditText
        android:id="@+id/centerText"
        android:layout_width="200dp"
        android:layout_centerInParent="true"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/rightButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"/>
</RelativeLayout>

topbar.java代码继承RelativeLayout :


import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;

public class TopBar extends RelativeLayout {

    private Button leftButton;
    private Button rightButton;
    private EditText titleTextView;
    private TypedArray typedArray;
    Setonclick setonclick;
    Context context;
    public TopBar(Context context) {
        super(context);
        this.context=context;
    }

    //回调接口方法
    public void Setonclickinterface(Setonclick setonclick){
        this.setonclick=setonclick;
    }

    //接口回调点击事件
    public interface Setonclick{
        void SetLeftButtonclick();
        void SetRightButtonclick();
    }

    public TopBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        //布局
        LayoutInflater.from(context).inflate(R.layout.topbar,this);
        //找控件
        leftButton = findViewById(R.id.leftButton);
        rightButton = findViewById(R.id.rightButton);
        titleTextView = findViewById(R.id.centerText);
        //左边点击事件
        leftButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setonclick.SetLeftButtonclick();
            }
        });
        //右边点击事件
        rightButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setonclick.SetRightButtonclick();
            }
        });
        //自定义view流程
        //创建属性文件attrs.xml
        //继承相关的类
        //实现构造方法
        //实现三个重要的方法,onMeasure:测量,onDraw:绘制,onLayout;布局位置摆放

        //获取属性文件并赋值
        typedArray = context.obtainStyledAttributes(attrs, R.styleable.TopBar);
        int leftButttonBackgrount = typedArray.getResourceId(R.styleable.TopBar_leftButttonBackgrount, 0);
        int rightButttonBackgrount = typedArray.getResourceId(R.styleable.TopBar_rightButttonBackgrount, 0);
        String titleText = typedArray.getString(R.styleable.TopBar_titleText);
        float titleTextSize = typedArray.getDimension(R.styleable.TopBar_titleTextSize, 0);
        int titleTextColor = typedArray.getColor(R.styleable.TopBar_titleTextColor, 0x38ad5a);
        //释放资源
        typedArray.recycle();
        //赋值
        leftButton.setBackgroundResource(leftButttonBackgrount);
        rightButton.setBackgroundResource(rightButttonBackgrount);
        titleTextView.setText(titleText);
        titleTextView.setTextSize(titleTextSize);
        titleTextView.setTextColor(titleTextColor);
    }

    public TopBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public TopBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

}

在Activity.xml使用topbar这个类就OK了

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_height="match_parent">
    <com.gy.week2.TopBar
        android:id="@+id/topbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:leftButttonBackgrount="@drawable/sel_11"
        app:rightButttonBackgrount="@drawable/sel_22"
        app:titleText="1608c"
        app:titleTextSize="30dp"
        app:titleTextColor="@color/colorPrimaryDark"></com.gy.week2.TopBar>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recy1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent">
        </android.support.v7.widget.RecyclerView>
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recy2"
            android:layout_width="0dp"
            android:layout_weight="3"
            android:layout_height="match_parent">
        </android.support.v7.widget.RecyclerView>
    </LinearLayout>
</LinearLayout>

猜你喜欢

转载自blog.csdn.net/gy1115/article/details/85943269