当前位置:   article > 正文

Android自定义控件之自定义组合控件_android 自定义控件 merge

android 自定义控件 merge

使用自定义组合控件的好处?

  • 提高布局文件开发效率
  • 降低布局文件维护成本
  • 降低布局文件和Activity代码耦合性
  • 容易扩展
  • 简单易用

如何实现一个自定义组合控件

1.)先定义一个布局文件

  1. <merge xmlns:android="http://schemas.android.com/apk/res/android">
  2. <Button
  3. android:id="@+id/title_bar_left"
  4. android:layout_width="wrap_content"
  5. android:layout_height="wrap_content"
  6. android:layout_alignParentLeft="true"
  7. android:layout_centerVertical="true"
  8. android:layout_marginLeft="5dp"
  9. android:background="@null"
  10. android:minHeight="45dp"
  11. android:minWidth="45dp"
  12. android:textSize="14sp" />
  13. <TextView
  14. android:id="@+id/title_bar_title"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:layout_centerInParent="true"
  18. android:singleLine="true"
  19. android:textSize="17sp" />
  20. <Button
  21. android:id="@+id/title_bar_right"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:layout_alignParentRight="true"
  25. android:layout_centerVertical="true"
  26. android:layout_marginRight="7dp"
  27. android:background="@null"
  28. android:minHeight="45dp"
  29. android:minWidth="45dp"
  30. android:textSize="14sp" />
  31. </merge>

注意:这里为何要使用merge标签,自定义组合控件时会继承RelativeLayout、LinearLayout等控件,这样导致布局的层级无形中增加了一层,如下是对比:

未使用merge标签

 

使用merge标签

 

2.)定义自定义属性

比如标题文字、标题栏左边按钮图标等。

  1. <declare-styleable name="CustomTitleBar">
  2. <attr name="title_background_color" format="reference|integer" />
  3. <attr name="left_button_visible" format="boolean" />
  4. <attr name="right_button_visible" format="boolean" />
  5. <attr name="title_text" format="string" />
  6. <attr name="title_text_color" format="color" />
  7. <attr name="title_text_drawable" format="reference|integer" />
  8. <attr name="right_button_text" format="string" />
  9. <attr name="right_button_text_color" format="color" />
  10. <attr name="right_button_drawable" format="reference|integer" />
  11. <attr name="left_button_text" format="string" />
  12. <attr name="left_button_text_color" format="color" />
  13. <attr name="left_button_drawable" format="reference|integer" />
  14. </declare-styleable

  3.)自定义一个View根据需求继承不同的ViewGroup子类,比如:RelativeLayout、LinearLayout等,我们这里继承RelativeLayout。

  1. public class CustomTitleBar extends RelativeLayout {
  2. private Button titleBarLeftBtn;
  3. private Button titleBarRightBtn;
  4. private TextView titleBarTitle;
  5. public CustomTitleBar(Context context, AttributeSet attrs) {
  6. super(context, attrs);
  7. LayoutInflater.from(context).inflate(R.layout.custom_title_bar, this, true);
  8. titleBarLeftBtn = (Button) findViewById(R.id.title_bar_left);
  9. titleBarRightBtn = (Button) findViewById(R.id.title_bar_right);
  10. titleBarTitle = (TextView) findViewById(R.id.title_bar_title);
  11. TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.CustomTitleBar);
  12. if (attributes != null) {
  13. //处理titleBar背景色
  14. int titleBarBackGround = attributes.getResourceId(R.styleable.CustomTitleBar_tlb_title_background_color, Color.GREEN);
  15. setBackgroundResource(titleBarBackGround);
  16. //先处理左边按钮
  17. //获取是否要显示左边按钮
  18. boolean leftButtonVisible = attributes.getBoolean(R.styleable.CustomTitleBar_tlb_left_button_visible, true);
  19. if (leftButtonVisible) {
  20. titleBarLeftBtn.setVisibility(View.VISIBLE);
  21. } else {
  22. titleBarLeftBtn.setVisibility(View.INVISIBLE);
  23. }
  24. //设置左边按钮的文字
  25. String leftButtonText = attributes.getString(R.styleable.CustomTitleBar_tlb_left_button_text);
  26. if (!TextUtils.isEmpty(leftButtonText)) {
  27. titleBarLeftBtn.setText(leftButtonText);
  28. //设置左边按钮文字颜色
  29. int leftButtonTextColor = attributes.getColor(R.styleable.CustomTitleBar_tlb_left_button_text_color, Color.WHITE);
  30. titleBarLeftBtn.setTextColor(leftButtonTextColor);
  31. }
  32. //设置左边图片icon 这里是二选一 要么只能是文字 要么只能是图片
  33. int leftButtonDrawable = attributes.getResourceId(R.styleable.CustomTitleBar_tlb_left_button_drawable, R.mipmap.titlebar_back_icon);
  34. if (leftButtonDrawable != -1) {
  35. titleBarLeftBtn.setCompoundDrawablesWithIntrinsicBounds(leftButtonDrawable, 0, 0, 0); //设置到哪个控件的位置()
  36. }
  37. //处理标题
  38. //先获取标题是否要显示图片icon
  39. int titleTextDrawable = attributes.getResourceId(R.styleable.CustomTitleBar_tlb_title_text_drawable, -1);
  40. if (titleTextDrawable != -1) {
  41. titleBarTitle.setBackgroundResource(titleTextDrawable);
  42. } else {
  43. //如果不是图片标题 则获取文字标题
  44. String titleText = attributes.getString(R.styleable.CustomTitleBar_tlb_title_text);
  45. if (!TextUtils.isEmpty(titleText)) {
  46. titleBarTitle.setText(titleText);
  47. }
  48. //获取标题显示颜色
  49. int titleTextColor = attributes.getColor(R.styleable.CustomTitleBar_tlb_title_text_color, Color.WHITE);
  50. titleBarTitle.setTextColor(titleTextColor);
  51. }
  52. //先处理右边按钮
  53. //获取是否要显示右边按钮
  54. boolean rightButtonVisible = attributes.getBoolean(R.styleable.CustomTitleBar_tlb_right_button_visible, true);
  55. if (rightButtonVisible) {
  56. titleBarRightBtn.setVisibility(View.VISIBLE);
  57. } else {
  58. titleBarRightBtn.setVisibility(View.INVISIBLE);
  59. }
  60. //设置右边按钮的文字
  61. String rightButtonText = attributes.getString(R.styleable.CustomTitleBar_tlb_right_button_text);
  62. if (!TextUtils.isEmpty(rightButtonText)) {
  63. titleBarRightBtn.setText(rightButtonText);
  64. //设置右边按钮文字颜色
  65. int rightButtonTextColor = attributes.getColor(R.styleable.CustomTitleBar_tlb_right_button_text_color, Color.WHITE);
  66. titleBarRightBtn.setTextColor(rightButtonTextColor);
  67. }
  68. //设置右边图片icon 这里是二选一 要么只能是文字 要么只能是图片
  69. int rightButtonDrawable = attributes.getResourceId(R.styleable.CustomTitleBar_tlb_right_button_drawable, -1);
  70. if (rightButtonDrawable != -1) {
  71. titleBarRightBtn.setCompoundDrawablesWithIntrinsicBounds(0, 0, rightButtonDrawable, 0); //设置到哪个控件的位置()
  72. }
  73. attributes.recycle();
  74. }
  75. }
  76. public void setTitleClickListener(OnClickListener onClickListener) {
  77. if (onClickListener != null) {
  78. titleBarLeftBtn.setOnClickListener(onClickListener);
  79. titleBarRightBtn.setOnClickListener(onClickListener);
  80. }
  81. }
  82. public Button getTitleBarLeftBtn() {
  83. return titleBarLeftBtn;
  84. }
  85. public Button getTitleBarRightBtn() {
  86. return titleBarRightBtn;
  87. }
  88. public TextView getTitleBarTitle() {
  89. return titleBarTitle;
  90. }
  91. }

4.)在不同的XML布局中引用

关于如何使用自定义属性这里就不再说明了,为了更加直观的查看效果,我这里在一个布局文件中实现不同要求的标题栏

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:lee="http://schemas.android.com/apk/res-auto"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical">
  7. <com.whoislcj.views.CustomTitleBar
  8. android:layout_width="match_parent"
  9. android:layout_height="45dp"
  10. android:layout_marginTop="10dp"
  11. lee:right_button_drawable="@mipmap/titlebar_add_icon"
  12. lee:title_background_color="@color/green"
  13. lee:title_text="标题1" />
  14. <com.whoislcj.views.CustomTitleBar
  15. android:layout_width="match_parent"
  16. android:layout_height="45dp"
  17. android:layout_marginTop="10dp"
  18. lee:right_button_visible="false"
  19. lee:title_background_color="@color/green"
  20. lee:title_text="标题2" />
  21. <com.whoislcj.views.CustomTitleBar
  22. android:layout_width="match_parent"
  23. android:layout_height="45dp"
  24. android:layout_marginTop="10dp"
  25. lee:left_button_text="左边"
  26. lee:right_button_text="右边"
  27. lee:title_background_color="@color/red"
  28. lee:title_text="标题3" />
  29. <com.whoislcj.views.CustomTitleBar
  30. android:layout_width="match_parent"
  31. android:layout_height="45dp"
  32. android:layout_marginTop="10dp"
  33. lee:left_button_text="左边"
  34. lee:right_button_drawable="@mipmap/titlebar_add_icon"
  35. lee:title_background_color="@color/red"
  36. lee:title_text="标题4" />
  37. <com.whoislcj.views.CustomTitleBar
  38. android:layout_width="match_parent"
  39. android:layout_height="45dp"
  40. android:layout_marginTop="10dp"
  41. lee:left_button_text="左边"
  42. lee:left_button_text_color="@color/red"
  43. lee:right_button_drawable="@mipmap/titlebar_add_icon"
  44. lee:title_background_color="@color/blue"
  45. lee:title_text="标题5" />
  46. <com.whoislcj.views.CustomTitleBar
  47. android:layout_width="match_parent"
  48. android:layout_height="45dp"
  49. android:layout_marginTop="10dp"
  50. lee:left_button_text="左边"
  51. lee:left_button_text_color="@color/red"
  52. lee:right_button_drawable="@mipmap/titlebar_add_icon"
  53. lee:title_background_color="@color/blue"
  54. lee:title_text="标题6"
  55. lee:title_text_color="@color/black" />
  56. </LinearLayout>

 显示效果:

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/759738
推荐阅读
相关标签
  

闽ICP备14008679号