1. 程式人生 > >android:自定義view--組合控制元件

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);
    }

    //設定右邊文字
    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");