当前位置: 首页 > 工具软件 > TitleBar > 使用案例 >

自定义View-TitleBar

雍志文
2023-12-01

title_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:background="@color/blue"
    android:orientation="vertical"
    tools:ignore="RtlHardcoded,RelativeOverlap">

    <TextView
        android:id="@+id/back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:background="@drawable/back" />

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textColor="@color/white"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/tip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:textColor="@color/white"
        android:textSize="16sp" />
</RelativeLayout>

colors.xml

<color name="blue">#606efd</color>
<color name="white">#FFFFFF</color>

attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

     <declare-styleable name="TitleBar">
        <attr name="titleText" format="string" />
        <attr name="tipText" format="string" />
        <attr name="titleTextSize" format="dimension" />
        <attr name="tipTextSize" format="dimension" />
        <attr name="titleTextColor" format="color" />
        <attr name="tipTextColor" format="color" />
        <attr name="barBackground" format="color" />
    </declare-styleable>

</resources>

TitleBar

public class TitleBar extends LinearLayout {

    private RelativeLayout bar;
    private TextView back;
    private TextView tip;
    private TextView title;
    /**
     * 主标题
     */
    private String titleBarTitleText;
    /**
     * 副标题
     */
    private String titleBarTipText;

    /**
     * 主标题字体大小
     */
    private float titleTextSize;
    /**
     * 副标题字体大小
     */
    private float tipTextSize;
    /**
     * 主标题字体颜色
     */
    private int titleTextColor;
    /**
     * 副标题字体颜色
     */
    private int tipTextColor;
    /**
     * 背景色
     */
    private int barBackground;


    public TitleBar(Context context) {
        super(context);
        initView(context);
    }

    public TitleBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initAttrs(context, attrs);
        initView(context);
        attrsToView();
    }

    public TitleBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttrs(context, attrs);
        initView(context);
        attrsToView();
    }

    /**
     * 格式初始化
     *
     * @param context 上下文
     * @param attrs   attrs文件
     */
    private void initAttrs(Context context, AttributeSet attrs) {
        TypedArray sa = context.obtainStyledAttributes(attrs, R.styleable.TitleBar);
        titleBarTitleText = sa.getString(R.styleable.TitleBar_titleText);
        titleBarTipText = sa.getString(R.styleable.TitleBar_tipText);
        titleTextSize = sa.getDimension(R.styleable.TitleBar_titleTextSize, 18);
        tipTextSize = sa.getDimension(R.styleable.TitleBar_tipTextSize, 16);
        titleTextColor = sa.getColor(R.styleable.TitleBar_titleTextColor, ContextCompat.getColor(context, R.color.white));
        tipTextColor = sa.getColor(R.styleable.TitleBar_tipTextColor, ContextCompat.getColor(context, R.color.white));
        barBackground = sa.getColor(R.styleable.TitleBar_barBackground, ContextCompat.getColor(context, R.color.blue));
        sa.recycle();
    }

    /**
     * 控件初始化
     *
     * @param context 上下文
     */
    private void initView(Context context) {
        LayoutInflater.from(context).inflate(R.layout.title_bar, this);
        bar = findViewById(R.id.bar);
        title = findViewById(R.id.title);
        back = findViewById(R.id.back);
        tip = findViewById(R.id.tip);
    }

    /**
     * 控件修改样式
     */
    private void attrsToView() {
        //背景颜色
        bar.setBackgroundColor(barBackground);
        //显示内容
        title.setText(titleBarTitleText);
        tip.setText(titleBarTipText);
        //字体大小
        title.setTextSize(titleTextSize);
        tip.setTextSize(tipTextSize);
        //字体颜色
        title.setTextColor(titleTextColor);
        tip.setTextColor(tipTextColor);
    }

    /**
     * 返回点击事件
     *
     * @param onClickListener 点击事件
     */
    public void setBackListener(OnClickListener onClickListener) {
        back.setOnClickListener(onClickListener);
    }

    /**
     * 副标题点击事件
     *
     * @param onClickListener 点击事件
     */
    public void setTipListener(OnClickListener onClickListener) {
        tip.setOnClickListener(onClickListener);
    }
}

调用

 <demo.bar.TitleBar
        android:id="@+id/titleBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:tipText="测试"
        app:titleText="@string/app_name" />
 TitleBar titleBar = findViewById(R.id.titleBar);
        titleBar.setBackListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        titleBar.setTipListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
 类似资料: