| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- package com.goldze.mvvmhabit.ui.view;
- import android.app.Activity;
- import android.content.Context;
- import android.content.res.TypedArray;
- import android.graphics.drawable.Drawable;
- import android.util.AttributeSet;
- import android.view.LayoutInflater;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.RelativeLayout;
- import android.widget.TextView;
- import com.goldze.mvvmhabit.R;
- import androidx.core.content.ContextCompat;
- /**
- * 标题控件
- */
- public class TemplateTitleBar extends RelativeLayout {
- private String titleText;
- private boolean canBack;
- private String backText;
- private int textColors;
- private String moreText;
- private LinearLayout backBtn;
- private int moreImg2;
- private int moreImg;
- private TextView tvTitle;
- private TextView txtBt;
- private boolean canLeft;
- // private int imgBackSrc;
- // private int defImgBackSrc;
- public TemplateTitleBar(Context context, AttributeSet attrs) {
- super(context, attrs);
- LayoutInflater.from(context).inflate(R.layout.widget_title, this);
- // defImgBackSrc = R.drawable.ui_return;
- TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TemplateTitleBar, 0, 0);
- try {
- titleText = ta.getString(R.styleable.TemplateTitleBar_titleText);
- canBack = ta.getBoolean(R.styleable.TemplateTitleBar_canBack, false);
- canLeft = ta.getBoolean(R.styleable.TemplateTitleBar_LeftText, false);
- backText = ta.getString(R.styleable.TemplateTitleBar_backText);
- moreText = ta.getString(R.styleable.TemplateTitleBar_moreText);
- moreImg2 = ta.getResourceId(R.styleable.TemplateTitleBar_moreImg, 0);
- textColors = ta.getResourceId(R.styleable.TemplateTitleBar_textColors, 0);
- moreImg = ta.getResourceId(R.styleable.TemplateTitleBar_addImg, 0);
- setUpView();
- } finally {
- ta.recycle();
- }
- }
- private void setUpView() {
- tvTitle = (TextView) findViewById(R.id.title);
- tvTitle.setText(titleText);
- backBtn = (LinearLayout) findViewById(R.id.title_back);
- txtBt = findViewById(R.id.txt_bt);
- TextView tvBack = (TextView) findViewById(R.id.txt_back);
- txtBt.setVisibility(canLeft ? VISIBLE : INVISIBLE);
- backBtn.setVisibility(canBack ? VISIBLE : GONE);
- if (canBack) {
- tvBack.setText(backText);
- }
- TextView tvMore2 = (TextView) findViewById(R.id.txt_more2);
- tvMore2.setText(moreText);
- if (textColors != 0) {
- tvTitle.setTextColor(ContextCompat.getColor(getContext(), textColors));
- tvBack.setTextColor(ContextCompat.getColor(getContext(), textColors));
- tvMore2.setTextColor(ContextCompat.getColor(getContext(), textColors));
- }
- if (moreImg2 != 0) {
- ImageView imgMore = findViewById(R.id.img_more2);
- imgMore.setImageDrawable(getResources().getDrawable(moreImg2));
- }
- if (moreImg != 0) {
- ImageView imgMore = findViewById(R.id.img_more);
- imgMore.setImageDrawable(getResources().getDrawable(moreImg));
- }
- backBtn.setOnClickListener(v -> {
- ((Activity) getContext()).finish();
- });
- }
- /**
- * 标题控件
- *
- * @param titleText 设置标题文案
- */
- public void setTitleText(String titleText) {
- this.titleText = titleText;
- TextView tvTitle = (TextView) findViewById(R.id.title);
- tvTitle.setText(titleText);
- }
- /**
- * 左侧图标 按钮
- *
- * @param img
- */
- public void setLifeImg(int img, OnClickListener listener) {
- ImageView moreImgView = (ImageView) findViewById(R.id.img_life);
- moreImgView.setImageDrawable(getContext().getResources().getDrawable(img));
- if (listener != null) {
- moreImgView.setOnClickListener(listener);
- }
- }
- public void setLeftText(int txt){
- txtBt.setText(getContext().getString(txt));
- }
- public void setLeftTextOnClick(OnClickListener listener){
- // txtBt.setVisibility(VISIBLE);
- txtBt.setOnClickListener(listener);
- }
- /**
- * 设置更多按钮事件
- *
- * @param listener 事件监听
- */
- public void setMoreImgAction(OnClickListener listener) {
- ImageView moreImgView = (ImageView) findViewById(R.id.img_more2);
- moreImgView.setVisibility(VISIBLE);
- if (listener != null) {
- moreImgView.setOnClickListener(listener);
- }
- }
- /**
- * 设置更多按钮事件
- *
- * @param listener 事件监听
- */
- public void setAddImgAction(OnClickListener listener) {
- ImageView moreImgView = (ImageView) findViewById(R.id.img_more);
- moreImgView.setVisibility(VISIBLE);
- if (listener != null) {
- moreImgView.setOnClickListener(listener);
- }
- }
- /**
- * 设置更多文字内容
- *
- * @param text 更多文本
- */
- public void setMoreTextContextAction(String text, OnClickListener listener) {
- TextView tvMore2 = (TextView) findViewById(R.id.txt_more2);
- tvMore2.setText(text);
- if (listener != null) {
- tvMore2.setOnClickListener(listener);
- }
- }
- /**
- * 设置更多文字内容
- *
- * @param listener 监听
- */
- public void setMoreTextListener(OnClickListener listener) {
- if (listener != null) {
- findViewById(R.id.txt_more2).setOnClickListener(listener);
- }
- }
- public void setTitleImage(int id, OnClickListener listener) {
- Drawable d = getResources().getDrawable(id);
- d.setBounds(0, 0, 50, 50); //必须设置图片大小,否则不显示
- tvTitle.setCompoundDrawables(null, null, d, null);
- tvTitle.setOnClickListener(listener);
- }
- /**
- * 设置右侧字体大小
- *
- * @param a
- */
- public void setMoreTextSize(float a) {
- TextView tvMore2 = (TextView) findViewById(R.id.txt_more2);
- tvMore2.setTextSize(a);
- }
- /**
- * 隐藏右侧按钮
- */
- public void getImgGone() {
- TextView tvMore2 = (TextView) findViewById(R.id.txt_more2);
- tvMore2.setVisibility(GONE);
- }
- /**
- * 设置返回文字内容
- *
- * @param text 更多文本
- */
- public void setTxtBackContextAction(String text, OnClickListener listener) {
- TextView txtBack = (TextView) findViewById(R.id.txt_back);
- txtBack.setText(text + " ");
- if (listener != null) {
- txtBack.setOnClickListener(listener);
- }
- txtBack.setVisibility(VISIBLE);
- LinearLayout backBtn = (LinearLayout) findViewById(R.id.title_back);
- backBtn.setVisibility(VISIBLE);
- // findViewById(R.id.img_back).setVisibility(GONE);
- }
- /**
- * 设置返回按钮事件
- *
- * @param listener 事件监听
- */
- public void setBackListener(OnClickListener listener) {
- if (canBack) {
- LinearLayout backBtn = (LinearLayout) findViewById(R.id.title_back);
- backBtn.setOnClickListener(listener);
- }
- }
- /**
- * s
- * 设置右侧图片
- */
- public void setRightImage(int img) {
- ImageView moreImgView = (ImageView) findViewById(R.id.img_more2);
- moreImgView.setVisibility(VISIBLE);
- moreImgView.setImageDrawable(getContext().getResources().getDrawable(img));
- }
- }
|