赞
踩
项目中用到了很多类似的界面,一行左右两边都是显示文本,数量非常多;
如果按照普通的方法画肯定也能非常轻松的画出来,但是因为使用地方较多,为了后期维护,代码的简介,提高开发效率,简单易用等等:可以自定义一个组合控件;
自定义组合控件使用起来非常方便,创建也非常的简单,四步走:
第一步:创建组合控件的XML布局文件
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
-
- <TextView
- android:id="@+id/tv_left"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="合同编号"
- android:textSize="12sp" />
-
- <TextView
- android:id="@+id/tv_right"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_weight="3"
- android:ellipsize="end"
- android:text="5896432569874"
- android:textSize="12sp" />
-
- </LinearLayout>

第二步:创建自定义属性
- <!--自定义组合控件样式:可在xml中直接设置(这里根据需求自己定义了四个属性,如果需要其他需求可自定义添加)-->
- <declare-styleable name="text_rollback">
- <!--左边文本-->
- <attr name="text_left" format="string" />
- <!--右边文本-->
- <attr name="text_right" format="string" />
- <!--右边文本颜色-->
- <attr name="text_color_right" format="color" />
- <!--右边文本最大行数-->
- <attr name="text_right_max_line" format="integer" />
- </declare-styleable>
第三步:自定义组合控件
- /**
- * Created by zheng on 2017/12/13.
- */
- public class CustomTextRollback extends LinearLayout{
-
- private TextView tvLeft, tvRight;
-
- public CustomTextRollback(Context context) {
- this(context,null);
- }
-
- public CustomTextRollback(Context context, @Nullable AttributeSet attrs) {
- super(context, attrs);
- LayoutInflater.from(context).inflate(R.layout.rollback_text_custom, this, true);
- tvLeft = (TextView) findViewById(R.id.tv_left);
- tvRight = (TextView) findViewById(R.id.tv_right);
-
- //获取自定义属性
- TypedArray attr = context.obtainStyledAttributes(attrs, R.styleable.text_rollback);
-
- if (attr == null) return;
- //设置左边文本
- String leftStr = attr.getString(R.styleable.text_rollback_text_left);
- tvLeft.setText(leftStr);
- //设置右边文本
- String rightStr = attr.getString(R.styleable.text_rollback_text_right);
- tvRight.setText(rightStr);
- //设置右边文本颜色
- int rightTextColor = attr.getColor(R.styleable.text_rollback_text_color_right, Color.rgb(66,66,66));
- tvRight.setTextColor(rightTextColor);
- //设置右边TextView最大行数
- int maxLine = attr.getInteger(R.styleable.text_rollback_text_right_max_line, 1);
- tvRight.setMaxLines(maxLine);
- attr.recycle();
- }
-
- //设置右边文本
- public void setRText(String txt) {
- tvRight.setText(txt);
- }
-
- //设置右边文本
- public void setRText(Spanned spanned) {
- tvRight.setText(spanned);
- }
-
- }

第四步:组合控件的使用(可在XML中引用,也可在代码中调用)
在XML中引用组合控件:
- <com.example.zheng.customgroupview.view.CustomTextRollback
- android:id="@+id/ctr_back_cause"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:paddingTop="10dp"
- app:text_color_right="@color/red_ff"
- app:text_left="退回原因"
- app:text_right="错号,不是本人,不认识本人"
- app:text_right_max_line="2" />
在代码中调用:
- ctrCompactNum= (CustomTextRollback) findViewById(R.id.ctr_compact_num);
- /**
- * 这里调用的方法都是提前在组合控件中设置好的,如果需要设置其他属性可自行增加
- */
- ctrCompactNum.setRText("代码设置的合同编号188128823883");
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。