765af15497722d890755e98aeff5492f1141728b.svn-base 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. if (array.hasValue(R.styleable.SettingBar_bar_leftColorHint)){
  63. setLeftColorHint(array.getColor(R.styleable.SettingBar_bar_leftColorHint, 0));
  64. }
  65. // 文字大小设置
  66. if (array.hasValue(R.styleable.SettingBar_bar_leftSize)) {
  67. setLeftSize(TypedValue.COMPLEX_UNIT_PX, array.getDimensionPixelSize(R.styleable.SettingBar_bar_leftSize, 0));
  68. }
  69. if (array.hasValue(R.styleable.SettingBar_bar_rightSize)) {
  70. setRightSize(TypedValue.COMPLEX_UNIT_PX, array.getDimensionPixelSize(R.styleable.SettingBar_bar_rightSize, 0));
  71. }
  72. // 分割线设置
  73. if (array.hasValue(R.styleable.SettingBar_bar_lineColor)) {
  74. setLineDrawable(array.getDrawable(R.styleable.SettingBar_bar_lineColor));
  75. }
  76. if (array.hasValue(R.styleable.SettingBar_bar_lineVisible)) {
  77. setLineVisible(array.getBoolean(R.styleable.SettingBar_bar_lineVisible, true));
  78. }
  79. if (array.hasValue(R.styleable.SettingBar_bar_lineSize)) {
  80. setLineSize(array.getDimensionPixelSize(R.styleable.SettingBar_bar_lineSize, 0));
  81. }
  82. if (array.hasValue(R.styleable.SettingBar_bar_lineMargin)) {
  83. setLineMargin(array.getDimensionPixelSize(R.styleable.SettingBar_bar_lineMargin, 0));
  84. }
  85. // 设置默认背景选择器
  86. if (getBackground() == null) {
  87. Drawable drawable = getContext().getResources().getDrawable(R.drawable.widget_bg_settting_bar_selector);
  88. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
  89. setBackground(drawable);
  90. }else {
  91. setBackgroundDrawable(drawable);
  92. }
  93. }
  94. // 回收TypedArray
  95. array.recycle();
  96. }
  97. /**
  98. * 设置左边的标题
  99. */
  100. public SettingBar setLeftText(int stringId) {
  101. return setLeftText(getResources().getString(stringId));
  102. }
  103. public SettingBar setLeftText(CharSequence text) {
  104. mLeftView.setText(text);
  105. return this;
  106. }
  107. public CharSequence getLeftText() {
  108. return mLeftView.getText();
  109. }
  110. /**
  111. * 设置左边的提示
  112. */
  113. public SettingBar setLeftHint(int stringId) {
  114. return setLeftHint(getResources().getString(stringId));
  115. }
  116. public SettingBar setLeftHint(CharSequence hint) {
  117. mLeftView.setHint(hint);
  118. return this;
  119. }
  120. /**
  121. * 设置右边的标题
  122. */
  123. public SettingBar setRightText(int stringId) {
  124. setRightText(getResources().getString(stringId));
  125. return this;
  126. }
  127. public SettingBar setRightText(CharSequence text) {
  128. mRightView.setText(text);
  129. return this;
  130. }
  131. public CharSequence getRightText() {
  132. return mRightView.getText();
  133. }
  134. /**
  135. * 设置右边的提示
  136. */
  137. public SettingBar setRightHint(int stringId) {
  138. return setRightHint(getResources().getString(stringId));
  139. }
  140. public SettingBar setRightHint(CharSequence hint) {
  141. mRightView.setHint(hint);
  142. return this;
  143. }
  144. /**
  145. * 设置左边的图标
  146. */
  147. public SettingBar setLeftIcon(int iconId) {
  148. if (iconId > 0) {
  149. setLeftIcon(getContext().getResources().getDrawable(iconId));
  150. }
  151. return this;
  152. }
  153. public SettingBar setLeftIcon(Drawable drawable) {
  154. mLeftView.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
  155. return this;
  156. }
  157. public Drawable getLeftIcon() {
  158. return mLeftView.getCompoundDrawables()[0];
  159. }
  160. /**
  161. * 设置右边的图标
  162. */
  163. public SettingBar setRightIcon(int iconId) {
  164. if (iconId > 0) {
  165. setRightIcon(getContext().getResources().getDrawable(iconId));
  166. }
  167. return this;
  168. }
  169. public SettingBar setRightIcon(Drawable drawable) {
  170. mRightView.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null);
  171. return this;
  172. }
  173. public Drawable getRightIcon() {
  174. return mRightView.getCompoundDrawables()[2];
  175. }
  176. /**
  177. * 设置左标题颜色
  178. */
  179. public SettingBar setLeftColor(int color) {
  180. mLeftView.setTextColor(color);
  181. return this;
  182. }
  183. /**
  184. * 设置左提示颜色
  185. */
  186. public SettingBar setLeftColorHint(int color) {
  187. mLeftView.setHintTextColor(color);
  188. return this;
  189. }
  190. /**
  191. * 设置右标题颜色
  192. */
  193. public SettingBar setRightColor(int color) {
  194. mRightView.setTextColor(color);
  195. return this;
  196. }
  197. /**
  198. * 设置左标题的文本大小
  199. */
  200. public SettingBar setLeftSize(int unit, float size) {
  201. mLeftView.setTextSize(unit, size);
  202. return this;
  203. }
  204. /**
  205. * 设置右标题的文本大小
  206. */
  207. public SettingBar setRightSize(int unit, float size) {
  208. mRightView.setTextSize(unit, size);
  209. return this;
  210. }
  211. /**
  212. * 设置分割线是否显示
  213. */
  214. public SettingBar setLineVisible(boolean visible) {
  215. mLineView.setVisibility(visible ? VISIBLE : GONE);
  216. return this;
  217. }
  218. /**
  219. * 设置分割线的颜色
  220. */
  221. public SettingBar setLineColor(int color) {
  222. return setLineDrawable(new ColorDrawable(color));
  223. }
  224. public SettingBar setLineDrawable(Drawable drawable) {
  225. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
  226. mLineView.setBackground(drawable);
  227. }else {
  228. mLineView.setBackgroundDrawable(drawable);
  229. }
  230. return this;
  231. }
  232. /**
  233. * 设置分割线的大小
  234. */
  235. public SettingBar setLineSize(int size) {
  236. ViewGroup.LayoutParams layoutParams = mLineView.getLayoutParams();
  237. layoutParams.height = size;
  238. mLineView.setLayoutParams(layoutParams);
  239. return this;
  240. }
  241. /**
  242. * 设置分割线边界
  243. */
  244. public SettingBar setLineMargin(int margin) {
  245. FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mLineView.getLayoutParams();
  246. params.leftMargin = margin;
  247. params.rightMargin = margin;
  248. mLineView.setLayoutParams(params);
  249. return this;
  250. }
  251. /**
  252. * 获取左标题View对象
  253. */
  254. public TextView getLeftView() {
  255. return mLeftView;
  256. }
  257. /**
  258. * 获取右标题View对象
  259. */
  260. public TextView getRightView() {
  261. return mRightView;
  262. }
  263. /**
  264. * 获取分割线View对象
  265. */
  266. public View getLineView() {
  267. return mLineView;
  268. }
  269. }