自定义titleLayout

 JavaCode:

package com.example.module_worldstudy.mvp.ui.widget;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.module_worldstudy.R;

/*
 * @Auther   xiaobo
 * @Date     2019/8/12       12:04
 * @Version  1.0
 * @Desc     自定义title
 */
public class CustomerTitle extends LinearLayout {
    private CharSequence title;
    private View view;
    private TextView tvTitle;
    private ImageView iv_back;
    private ImageView iv_right;

    public CustomerTitle(Context context) {
        this(context, null);
    }

    public CustomerTitle(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public CustomerTitle(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
        initAttr(context, attrs);
        initListner(context);
    }

    private void initListner(Context context) {
        iv_back.setOnClickListener(v -> {
            if(context instanceof Activity){
                ((Activity) context).finish();
            }
        });
    }

    /**
     * 左边按钮点击事件
     * @param onLeftImageListener  事件监听
     */
    public void setOnLeftImageListener(OnClickListener onLeftImageListener){
        iv_back.setOnClickListener(onLeftImageListener);
    }


    /**
     * 右边按钮点击事件
     * @param onRightImageListener  事件监听
     */
    public void setOnRightImageListener(OnClickListener onRightImageListener){
        iv_right.setOnClickListener(onRightImageListener);
    }

    //初始化自定义属性
    private void initAttr(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomerTitle);
        int backGroundColor = typedArray.getColor(R.styleable.CustomerTitle_bgGround, -1);
        view.setBackgroundColor(backGroundColor);
        Drawable leftImg = typedArray.getDrawable(R.styleable.CustomerTitle_left_img);
        float textSize = typedArray.getInt(R.styleable.CustomerTitle_title_text_size, 14);
        int titleTextColor = typedArray.getInt(R.styleable.CustomerTitle_title_text_color,-1);
        if (leftImg == null) {
            iv_back.setImageDrawable(getResources().getDrawable(R.drawable.back));
        } else {
            iv_back.setImageDrawable(leftImg);
        }
        Drawable rightImg = typedArray.getDrawable(R.styleable.CustomerTitle_right_img);
        if (rightImg != null) {
            iv_right.setImageDrawable(rightImg);
        }else{
            iv_right.setVisibility(GONE);
        }
        title = typedArray.getText(R.styleable.CustomerTitle_title);
        tvTitle.setText(title);
        tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP,textSize);
        tvTitle.setTextColor(titleTextColor);
        typedArray.recycle();
    }

    /**
     * 设置左边图片
     * @param leftImageResource 左边imageResouce
     */
    public void setLeftImageResource(int leftImageResource){
        iv_back.setImageResource(leftImageResource);
    }

    /**
     * 设置右边图片
     * @param rightImageResource 右边imageResouce
     */
    public void setRightImageResource(int rightImageResource){
        iv_right.setImageResource(rightImageResource);
    }

    /**
     * 设置title 文字
     * @param title title 文字
     */
    public void setTitleText(String title){
        if(tvTitle != null){
            tvTitle.setText(title);
        }
    }

    public void setTitleSize(int size){
        if(tvTitle != null){
            tvTitle.setTextSize(size);
        }
    }

    /**
     * 设置title 文字颜色
     * @param color 文字颜色
     */
    public void setTitleTextColor(int color){
        if(tvTitle != null){
            tvTitle.setTextColor(color);
        }
    }


    //初始化View
    private void initView(Context context) {
        view = LayoutInflater.from(context).inflate(R.layout.panel_title, this);
        iv_back = view.findViewById(R.id.iv_back);
        iv_right = view.findViewById(R.id.iv_right);
        tvTitle = view.findViewById(R.id.tvTitle);
    }


}

attr:

    <!--自定义title自定义属性-->
    <declare-styleable name="CustomerTitle">
        <attr name="bgGround" format="color|reference"/>
        <attr name="height" format="dimension"/>
        <attr name="left_img" format="reference"/>
        <attr name="right_img" format="reference"/>
        <attr name="title" format="string"/>
        <attr name="title_text_size" format="integer"/>
        <attr name="title_text_color" format="color"/>
    </declare-styleable>

xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rootView"
    android:layout_width="match_parent"
    android:layout_height="@dimen/public_height_48dp"
    android:gravity="center_vertical"
    android:padding="@dimen/public_5dp">

    <ImageView
        android:id="@+id/iv_back"
        android:layout_width="@dimen/public_40dp"
        android:layout_height="@dimen/public_30dp"
        android:padding="@dimen/public_8dp" />



    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:ellipsize="end"
        android:gravity="center"
        android:lines="1"
        android:text="" />
    <LinearLayout
        android:layout_width="@dimen/public_30dp"
        android:layout_height="@dimen/public_30dp"
        android:layout_marginRight="@dimen/public_15dp"
        android:gravity="center_vertical">
        <ImageView
            android:id="@+id/iv_right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
</LinearLayout>

使用:

  <com.xx.xx.xxx.CustomerTitle
        android:layout_width="match_parent"
        android:layout_height="@dimen/public_height_48dp"
        customer:title="text"
        customer:title_text_size="18"
        customer:bgGround="@color/colorPrimary"
        customer:title_text_color="@color/white"
        />

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值