当前位置:   article > 正文

android:自定义view--组合控件_android 组合控件

android 组合控件

项目中用到了很多类似的界面,一行左右两边都是显示文本,数量非常多;

如果按照普通的方法画肯定也能非常轻松的画出来,但是因为使用地方较多,为了后期维护,代码的简介,提高开发效率,简单易用等等:可以自定义一个组合控件;

自定义组合控件使用起来非常方便,创建也非常的简单,四步走:

第一步:创建组合控件的XML布局文件

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="wrap_content"
  4. android:orientation="horizontal">
  5. <TextView
  6. android:id="@+id/tv_left"
  7. android:layout_width="0dp"
  8. android:layout_height="wrap_content"
  9. android:layout_weight="1"
  10. android:text="合同编号"
  11. android:textSize="12sp" />
  12. <TextView
  13. android:id="@+id/tv_right"
  14. android:layout_width="0dp"
  15. android:layout_height="wrap_content"
  16. android:layout_weight="3"
  17. android:ellipsize="end"
  18. android:text="5896432569874"
  19. android:textSize="12sp" />
  20. </LinearLayout>


第二步:创建自定义属性

  1. <!--自定义组合控件样式:可在xml中直接设置(这里根据需求自己定义了四个属性,如果需要其他需求可自定义添加)-->
  2. <declare-styleable name="text_rollback">
  3. <!--左边文本-->
  4. <attr name="text_left" format="string" />
  5. <!--右边文本-->
  6. <attr name="text_right" format="string" />
  7. <!--右边文本颜色-->
  8. <attr name="text_color_right" format="color" />
  9. <!--右边文本最大行数-->
  10. <attr name="text_right_max_line" format="integer" />
  11. </declare-styleable>


第三步:自定义组合控件

  1. /**
  2. * Created by zheng on 2017/12/13.
  3. */
  4. public class CustomTextRollback extends LinearLayout{
  5. private TextView tvLeft, tvRight;
  6. public CustomTextRollback(Context context) {
  7. this(context,null);
  8. }
  9. public CustomTextRollback(Context context, @Nullable AttributeSet attrs) {
  10. super(context, attrs);
  11. LayoutInflater.from(context).inflate(R.layout.rollback_text_custom, this, true);
  12. tvLeft = (TextView) findViewById(R.id.tv_left);
  13. tvRight = (TextView) findViewById(R.id.tv_right);
  14. //获取自定义属性
  15. TypedArray attr = context.obtainStyledAttributes(attrs, R.styleable.text_rollback);
  16. if (attr == null) return;
  17. //设置左边文本
  18. String leftStr = attr.getString(R.styleable.text_rollback_text_left);
  19. tvLeft.setText(leftStr);
  20. //设置右边文本
  21. String rightStr = attr.getString(R.styleable.text_rollback_text_right);
  22. tvRight.setText(rightStr);
  23. //设置右边文本颜色
  24. int rightTextColor = attr.getColor(R.styleable.text_rollback_text_color_right, Color.rgb(66,66,66));
  25. tvRight.setTextColor(rightTextColor);
  26. //设置右边TextView最大行数
  27. int maxLine = attr.getInteger(R.styleable.text_rollback_text_right_max_line, 1);
  28. tvRight.setMaxLines(maxLine);
  29. attr.recycle();
  30. }
  31. //设置右边文本
  32. public void setRText(String txt) {
  33. tvRight.setText(txt);
  34. }
  35. //设置右边文本
  36. public void setRText(Spanned spanned) {
  37. tvRight.setText(spanned);
  38. }
  39. }

第四步:组合控件的使用(可在XML中引用,也可在代码中调用)

在XML中引用组合控件:

  1. <com.example.zheng.customgroupview.view.CustomTextRollback
  2. android:id="@+id/ctr_back_cause"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:paddingTop="10dp"
  6. app:text_color_right="@color/red_ff"
  7. app:text_left="退回原因"
  8. app:text_right="错号,不是本人,不认识本人"
  9. app:text_right_max_line="2" />


在代码中调用:

  1. ctrCompactNum= (CustomTextRollback) findViewById(R.id.ctr_compact_num);
  2. /**
  3. * 这里调用的方法都是提前在组合控件中设置好的,如果需要设置其他属性可自行增加
  4. */
  5. ctrCompactNum.setRText("代码设置的合同编号188128823883");


 

点击打开链接下载源码

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

闽ICP备14008679号