TemplateTitleBar.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. package com.goldze.mvvmhabit.ui.view;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.content.res.TypedArray;
  5. import android.graphics.drawable.Drawable;
  6. import android.util.AttributeSet;
  7. import android.view.LayoutInflater;
  8. import android.widget.ImageView;
  9. import android.widget.LinearLayout;
  10. import android.widget.RelativeLayout;
  11. import android.widget.TextView;
  12. import com.goldze.mvvmhabit.R;
  13. import androidx.core.content.ContextCompat;
  14. /**
  15. * 标题控件
  16. */
  17. public class TemplateTitleBar extends RelativeLayout {
  18. private String titleText;
  19. private boolean canBack;
  20. private String backText;
  21. private int textColors;
  22. private String moreText;
  23. private LinearLayout backBtn;
  24. private int moreImg2;
  25. private int moreImg;
  26. private TextView tvTitle;
  27. private TextView txtBt;
  28. private boolean canLeft;
  29. // private int imgBackSrc;
  30. // private int defImgBackSrc;
  31. public TemplateTitleBar(Context context, AttributeSet attrs) {
  32. super(context, attrs);
  33. LayoutInflater.from(context).inflate(R.layout.widget_title, this);
  34. // defImgBackSrc = R.drawable.ui_return;
  35. TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TemplateTitleBar, 0, 0);
  36. try {
  37. titleText = ta.getString(R.styleable.TemplateTitleBar_titleText);
  38. canBack = ta.getBoolean(R.styleable.TemplateTitleBar_canBack, false);
  39. canLeft = ta.getBoolean(R.styleable.TemplateTitleBar_LeftText, false);
  40. backText = ta.getString(R.styleable.TemplateTitleBar_backText);
  41. moreText = ta.getString(R.styleable.TemplateTitleBar_moreText);
  42. moreImg2 = ta.getResourceId(R.styleable.TemplateTitleBar_moreImg, 0);
  43. textColors = ta.getResourceId(R.styleable.TemplateTitleBar_textColors, 0);
  44. moreImg = ta.getResourceId(R.styleable.TemplateTitleBar_addImg, 0);
  45. setUpView();
  46. } finally {
  47. ta.recycle();
  48. }
  49. }
  50. private void setUpView() {
  51. tvTitle = (TextView) findViewById(R.id.title);
  52. tvTitle.setText(titleText);
  53. backBtn = (LinearLayout) findViewById(R.id.title_back);
  54. txtBt = findViewById(R.id.txt_bt);
  55. TextView tvBack = (TextView) findViewById(R.id.txt_back);
  56. txtBt.setVisibility(canLeft ? VISIBLE : INVISIBLE);
  57. backBtn.setVisibility(canBack ? VISIBLE : GONE);
  58. if (canBack) {
  59. tvBack.setText(backText);
  60. }
  61. TextView tvMore2 = (TextView) findViewById(R.id.txt_more2);
  62. tvMore2.setText(moreText);
  63. if (textColors != 0) {
  64. tvTitle.setTextColor(ContextCompat.getColor(getContext(), textColors));
  65. tvBack.setTextColor(ContextCompat.getColor(getContext(), textColors));
  66. tvMore2.setTextColor(ContextCompat.getColor(getContext(), textColors));
  67. }
  68. if (moreImg2 != 0) {
  69. ImageView imgMore = findViewById(R.id.img_more2);
  70. imgMore.setImageDrawable(getResources().getDrawable(moreImg2));
  71. }
  72. if (moreImg != 0) {
  73. ImageView imgMore = findViewById(R.id.img_more);
  74. imgMore.setImageDrawable(getResources().getDrawable(moreImg));
  75. }
  76. backBtn.setOnClickListener(v -> {
  77. ((Activity) getContext()).finish();
  78. });
  79. }
  80. /**
  81. * 标题控件
  82. *
  83. * @param titleText 设置标题文案
  84. */
  85. public void setTitleText(String titleText) {
  86. this.titleText = titleText;
  87. TextView tvTitle = (TextView) findViewById(R.id.title);
  88. tvTitle.setText(titleText);
  89. }
  90. /**
  91. * 左侧图标 按钮
  92. *
  93. * @param img
  94. */
  95. public void setLifeImg(int img, OnClickListener listener) {
  96. ImageView moreImgView = (ImageView) findViewById(R.id.img_life);
  97. moreImgView.setImageDrawable(getContext().getResources().getDrawable(img));
  98. if (listener != null) {
  99. moreImgView.setOnClickListener(listener);
  100. }
  101. }
  102. public void setLeftText(int txt){
  103. txtBt.setText(getContext().getString(txt));
  104. }
  105. public void setLeftTextOnClick(OnClickListener listener){
  106. // txtBt.setVisibility(VISIBLE);
  107. txtBt.setOnClickListener(listener);
  108. }
  109. /**
  110. * 设置更多按钮事件
  111. *
  112. * @param listener 事件监听
  113. */
  114. public void setMoreImgAction(OnClickListener listener) {
  115. ImageView moreImgView = (ImageView) findViewById(R.id.img_more2);
  116. moreImgView.setVisibility(VISIBLE);
  117. if (listener != null) {
  118. moreImgView.setOnClickListener(listener);
  119. }
  120. }
  121. /**
  122. * 设置更多按钮事件
  123. *
  124. * @param listener 事件监听
  125. */
  126. public void setAddImgAction(OnClickListener listener) {
  127. ImageView moreImgView = (ImageView) findViewById(R.id.img_more);
  128. moreImgView.setVisibility(VISIBLE);
  129. if (listener != null) {
  130. moreImgView.setOnClickListener(listener);
  131. }
  132. }
  133. /**
  134. * 设置更多文字内容
  135. *
  136. * @param text 更多文本
  137. */
  138. public void setMoreTextContextAction(String text, OnClickListener listener) {
  139. TextView tvMore2 = (TextView) findViewById(R.id.txt_more2);
  140. tvMore2.setText(text);
  141. if (listener != null) {
  142. tvMore2.setOnClickListener(listener);
  143. }
  144. }
  145. /**
  146. * 设置更多文字内容
  147. *
  148. * @param listener 监听
  149. */
  150. public void setMoreTextListener(OnClickListener listener) {
  151. if (listener != null) {
  152. findViewById(R.id.txt_more2).setOnClickListener(listener);
  153. }
  154. }
  155. public void setTitleImage(int id, OnClickListener listener) {
  156. Drawable d = getResources().getDrawable(id);
  157. d.setBounds(0, 0, 50, 50); //必须设置图片大小,否则不显示
  158. tvTitle.setCompoundDrawables(null, null, d, null);
  159. tvTitle.setOnClickListener(listener);
  160. }
  161. /**
  162. * 设置右侧字体大小
  163. *
  164. * @param a
  165. */
  166. public void setMoreTextSize(float a) {
  167. TextView tvMore2 = (TextView) findViewById(R.id.txt_more2);
  168. tvMore2.setTextSize(a);
  169. }
  170. /**
  171. * 隐藏右侧按钮
  172. */
  173. public void getImgGone() {
  174. TextView tvMore2 = (TextView) findViewById(R.id.txt_more2);
  175. tvMore2.setVisibility(GONE);
  176. }
  177. /**
  178. * 设置返回文字内容
  179. *
  180. * @param text 更多文本
  181. */
  182. public void setTxtBackContextAction(String text, OnClickListener listener) {
  183. TextView txtBack = (TextView) findViewById(R.id.txt_back);
  184. txtBack.setText(text + " ");
  185. if (listener != null) {
  186. txtBack.setOnClickListener(listener);
  187. }
  188. txtBack.setVisibility(VISIBLE);
  189. LinearLayout backBtn = (LinearLayout) findViewById(R.id.title_back);
  190. backBtn.setVisibility(VISIBLE);
  191. // findViewById(R.id.img_back).setVisibility(GONE);
  192. }
  193. /**
  194. * 设置返回按钮事件
  195. *
  196. * @param listener 事件监听
  197. */
  198. public void setBackListener(OnClickListener listener) {
  199. if (canBack) {
  200. LinearLayout backBtn = (LinearLayout) findViewById(R.id.title_back);
  201. backBtn.setOnClickListener(listener);
  202. }
  203. }
  204. /**
  205. * s
  206. * 设置右侧图片
  207. */
  208. public void setRightImage(int img) {
  209. ImageView moreImgView = (ImageView) findViewById(R.id.img_more2);
  210. moreImgView.setVisibility(VISIBLE);
  211. moreImgView.setImageDrawable(getContext().getResources().getDrawable(img));
  212. }
  213. }