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

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

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

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

第一步:创建组合控件的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");


 

点击打开链接下载源码

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值