31a62346c9daf262c01d568cbfdf8ccfb637ff6c.svn-base 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. package com.hjq.widget;
  2. import android.content.Context;
  3. import android.content.res.TypedArray;
  4. import android.graphics.drawable.ColorDrawable;
  5. import android.graphics.drawable.Drawable;
  6. import android.os.Build;
  7. import android.util.AttributeSet;
  8. import android.util.TypedValue;
  9. import android.view.LayoutInflater;
  10. import android.view.View;
  11. import android.view.ViewGroup;
  12. import android.widget.FrameLayout;
  13. import android.widget.TextView;
  14. /**
  15. * desc : 设置条自定义控件
  16. */
  17. public final class SettingBar extends FrameLayout {
  18. private TextView mLeftView;
  19. private TextView mRightView;
  20. private View mLineView;
  21. public SettingBar(Context context) {
  22. this(context, null);
  23. }
  24. public SettingBar(Context context, AttributeSet attrs) {
  25. this(context, attrs, 0);
  26. }
  27. public SettingBar(Context context, AttributeSet attrs, int defStyleAttr) {
  28. super(context, attrs, defStyleAttr);
  29. LayoutInflater.from(context).inflate(R.layout.widget_setting_bar, this);
  30. mLeftView = (TextView) findViewById(R.id.tv_setting_bar_left);
  31. mRightView = (TextView) findViewById(R.id.tv_setting_bar_right);
  32. mLineView = (View) findViewById(R.id.v_setting_bar_line);
  33. final TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.SettingBar);
  34. // 文本设置
  35. if (array.hasValue(R.styleable.SettingBar_bar_leftText)) {
  36. setLeftText(array.getString(R.styleable.SettingBar_bar_leftText));
  37. }
  38. if (array.hasValue(R.styleable.SettingBar_bar_rightText)) {
  39. setRightText(array.getString(R.styleable.SettingBar_bar_rightText));
  40. }
  41. // 提示设置
  42. if (array.hasValue(R.styleable.SettingBar_bar_leftHint)) {
  43. setLeftHint(array.getString(R.styleable.SettingBar_bar_leftHint));
  44. }
  45. if (array.hasValue(R.styleable.SettingBar_bar_rightHint)) {
  46. setRightHint(array.getString(R.styleable.SettingBar_bar_rightHint));
  47. }
  48. // 图标设置
  49. if (array.hasValue(R.styleable.SettingBar_bar_leftIcon)) {
  50. setLeftIcon(getContext().getResources().getDrawable(array.getResourceId(R.styleable.SettingBar_bar_leftIcon, 0)));
  51. }
  52. if (array.hasValue(R.styleable.SettingBar_bar_rightIcon)) {
  53. setRightIcon(getContext().getResources().getDrawable(array.getResourceId(R.styleable.SettingBar_bar_rightIcon, 0)));
  54. }
  55. // 文字颜色设置
  56. if (array.hasValue(R.styleable.SettingBar_bar_leftColor)) {
  57. setLeftColor(array.getColor(R.styleable.SettingBar_bar_leftColor, 0));
  58. }
  59. if (array.hasValue(R.styleable.SettingBar_bar_rightColor)) {
  60. setRightColor(array.getColor(R.styleable.SettingBar_bar_rightColor, 0));
  61. }
  62. // 文字大小设置
  63. if (array.hasValue(R.styleable.SettingBar_bar_leftSize)) {
  64. setLeftSize(TypedValue.COMPLEX_UNIT_PX, array.getDimensionPixelSize(R.styleable.SettingBar_bar_leftSize, 0));
  65. }
  66. if (array.hasValue(R.styleable.SettingBar_bar_rightSize)) {
  67. setRightSize(TypedValue.COMPLEX_UNIT_PX, array.getDimensionPixelSize(R.styleable.SettingBar_bar_rightSize, 0));
  68. }
  69. // 分割线设置
  70. if (array.hasValue(R.styleable.SettingBar_bar_lineColor)) {
  71. setLineDrawable(array.getDrawable(R.styleable.SettingBar_bar_lineColor));
  72. }
  73. if (array.hasValue(R.styleable.SettingBar_bar_lineVisible)) {
  74. setLineVisible(array.getBoolean(R.styleable.SettingBar_bar_lineVisible, true));
  75. }
  76. if (array.hasValue(R.styleable.SettingBar_bar_lineSize)) {
  77. setLineSize(array.getDimensionPixelSize(R.styleable.SettingBar_bar_lineSize, 0));
  78. }
  79. if (array.hasValue(R.styleable.SettingBar_bar_lineMargin)) {
  80. setLineMargin(array.getDimensionPixelSize(R.styleable.SettingBar_bar_lineMargin, 0));
  81. }
  82. // 设置默认背景选择器
  83. if (getBackground() == null) {
  84. Drawable drawable = getContext().getResources().getDrawable(R.drawable.widget_bg_settting_bar_selector);
  85. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
  86. setBackground(drawable);
  87. }else {
  88. setBackgroundDrawable(drawable);
  89. }
  90. }
  91. // 回收TypedArray
  92. array.recycle();
  93. }
  94. /**
  95. * 设置左边的标题
  96. */
  97. public SettingBar setLeftText(int stringId) {
  98. return setLeftText(getResources().getString(stringId));
  99. }
  100. public SettingBar setLeftText(CharSequence text) {
  101. mLeftView.setText(text);
  102. return this;
  103. }
  104. public CharSequence getLeftText() {
  105. return mLeftView.getText();
  106. }
  107. /**
  108. * 设置左边的提示
  109. */
  110. public SettingBar setLeftHint(int stringId) {
  111. return setLeftHint(getResources().getString(stringId));
  112. }
  113. public SettingBar setLeftHint(CharSequence hint) {
  114. mLeftView.setHint(hint);
  115. return this;
  116. }
  117. /**
  118. * 设置右边的标题
  119. */
  120. public SettingBar setRightText(int stringId) {
  121. setRightText(getResources().getString(stringId));
  122. return this;
  123. }
  124. public SettingBar setRightText(CharSequence text) {
  125. mRightView.setText(text);
  126. return this;
  127. }
  128. public CharSequence getRightText() {
  129. return mRightView.getText();
  130. }
  131. /**
  132. * 设置右边的提示
  133. */
  134. public SettingBar setRightHint(int stringId) {
  135. return setRightHint(getResources().getString(stringId));
  136. }
  137. public SettingBar setRightHint(CharSequence hint) {
  138. mRightView.setHint(hint);
  139. return this;
  140. }
  141. /**
  142. * 设置左边的图标
  143. */
  144. public SettingBar setLeftIcon(int iconId) {
  145. if (iconId > 0) {
  146. setLeftIcon(getContext().getResources().getDrawable(iconId));
  147. }
  148. return this;
  149. }
  150. public SettingBar setLeftIcon(Drawable drawable) {
  151. mLeftView.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
  152. return this;
  153. }
  154. public Drawable getLeftIcon() {
  155. return mLeftView.getCompoundDrawables()[0];
  156. }
  157. /**
  158. * 设置右边的图标
  159. */
  160. public SettingBar setRightIcon(int iconId) {
  161. if (iconId > 0) {
  162. setRightIcon(getContext().getResources().getDrawable(iconId));
  163. }
  164. return this;
  165. }
  166. public SettingBar setRightIcon(Drawable drawable) {
  167. mRightView.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null);
  168. return this;
  169. }
  170. public Drawable getRightIcon() {
  171. return mRightView.getCompoundDrawables()[2];
  172. }
  173. /**
  174. * 设置左标题颜色
  175. */
  176. public SettingBar setLeftColor(int color) {
  177. mLeftView.setTextColor(color);
  178. return this;
  179. }
  180. /**
  181. * 设置右标题颜色
  182. */
  183. public SettingBar setRightColor(int color) {
  184. mRightView.setTextColor(color);
  185. return this;
  186. }
  187. /**
  188. * 设置左标题的文本大小
  189. */
  190. public SettingBar setLeftSize(int unit, float size) {
  191. mLeftView.setTextSize(unit, size);
  192. return this;
  193. }
  194. /**
  195. * 设置右标题的文本大小
  196. */
  197. public SettingBar setRightSize(int unit, float size) {
  198. mRightView.setTextSize(unit, size);
  199. return this;
  200. }
  201. /**
  202. * 设置分割线是否显示
  203. */
  204. public SettingBar setLineVisible(boolean visible) {
  205. mLineView.setVisibility(visible ? VISIBLE : GONE);
  206. return this;
  207. }
  208. /**
  209. * 设置分割线的颜色
  210. */
  211. public SettingBar setLineColor(int color) {
  212. return setLineDrawable(new ColorDrawable(color));
  213. }
  214. public SettingBar setLineDrawable(Drawable drawable) {
  215. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
  216. mLineView.setBackground(drawable);
  217. }else {
  218. mLineView.setBackgroundDrawable(drawable);
  219. }
  220. return this;
  221. }
  222. /**
  223. * 设置分割线的大小
  224. */
  225. public SettingBar setLineSize(int size) {
  226. ViewGroup.LayoutParams layoutParams = mLineView.getLayoutParams();
  227. layoutParams.height = size;
  228. mLineView.setLayoutParams(layoutParams);
  229. return this;
  230. }
  231. /**
  232. * 设置分割线边界
  233. */
  234. public SettingBar setLineMargin(int margin) {
  235. FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mLineView.getLayoutParams();
  236. params.leftMargin = margin;
  237. params.rightMargin = margin;
  238. mLineView.setLayoutParams(params);
  239. return this;
  240. }
  241. /**
  242. * 获取左标题View对象
  243. */
  244. public TextView getLeftView() {
  245. return mLeftView;
  246. }
  247. /**
  248. * 获取右标题View对象
  249. */
  250. public TextView getRightView() {
  251. return mRightView;
  252. }
  253. /**
  254. * 获取分割线View对象
  255. */
  256. public View getLineView() {
  257. return mLineView;
  258. }
  259. }