自定義組合控件

這個控件可作為表單的item??梢宰远x左右兩邊的文字顏色和大小。
先看效果

device-2017-04-07-112342.png

1.布局文件
根布局使用merge減少層級

  <?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="44dp"
        android:orientation="horizontal"
        android:gravity="center_vertical">

        <TextView
            android:id="@+id/tvLeft_itemchooseview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/tvRight_itemchooseview"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:gravity="end"
            android:layout_height="wrap_content" />

        <ImageView
            android:id="@+id/imgRight_itemchooseview"
            android:layout_width="15dp"
            android:layout_height="15dp"
            android:layout_marginLeft="4dp"
            android:src="@mipmap/arrow_right_yellow"/>
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#ccc"
        android:layout_alignParentBottom="true" />
</merge>

2.繼承相對布局

public class ItemChooseView extends RelativeLayout {
    private final static int DEFAULT_LEFT_TEXTSIZE=15;
    private final static int DEFAULT_RIGHT_TEXTSIZE=15;
    private final static int DEFAULT_LEFT_COLOR=Color.BLACK;
    private final static int DEFAULT_RIGHT_COLOR=Color.BLUE;
    private CharSequence textLeft;
    private CharSequence textRigth;
    private int textLeftColor;
    private int textRightColor;
    private float textLeftTextSize;
    private float textRightTextSize;
    private TextView textviewLeft;
    private TextView textviewRight;

    public ItemChooseView(@NonNull Context context) {
        this(context,null);
    }

    public ItemChooseView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ItemChooseView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        inflate(getContext(), R.layout.itemchoose_layout, this);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ItemChooseView);
        //左邊文字
        textLeft = array.getText(R.styleable.ItemChooseView_textLeft);
        //右邊文字
        textRigth = array.getText(R.styleable.ItemChooseView_textRight);
        //左邊文字顏色
        textLeftColor = array.getColor(R.styleable.ItemChooseView_textLeftColor,
                DEFAULT_LEFT_COLOR);
        //右邊文字顏色
        textRightColor = array.getColor(R.styleable.ItemChooseView_textRightColor,
                DEFAULT_RIGHT_COLOR);
        //左邊文字大小
        textLeftTextSize = array.getDimension(R.styleable.ItemChooseView_textLeftTextSize,
                DEFAULT_LEFT_TEXTSIZE);
        //右邊文字大小
        textRightTextSize = array.getDimension(R.styleable.ItemChooseView_textRightTextSize,
                DEFAULT_RIGHT_TEXTSIZE);
        array.recycle();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        textviewLeft = (TextView) findViewById(R.id.tvLeft_itemchooseview);
        textviewRight = (TextView) findViewById(R.id.tvRight_itemchooseview);
        textviewLeft.setText(textLeft);
        if(textLeftTextSize==DEFAULT_LEFT_TEXTSIZE) {
            textviewLeft.setTextSize(textLeftTextSize);
        }else{
            textviewLeft.getPaint().setTextSize(textLeftTextSize);
        }
        textviewLeft.setTextColor(textLeftColor);

        textviewRight.setText(textRigth);
        if(textRightTextSize==DEFAULT_RIGHT_TEXTSIZE) {
            textviewRight.setTextSize(textRightTextSize);
        }else{
            textviewRight.getPaint().setTextSize(textRightTextSize);
        }
        textviewRight.setTextColor(textRightColor);
    }
    public void setTextviewLeft(CharSequence charSequence){
        textviewLeft.setText(charSequence);
    }
    public CharSequence getTextViewLeft(){
        return textviewLeft.getText();
    }
    public void setTextViewRight(CharSequence charSequence){
        textviewRight.setText(charSequence);
    }
    public CharSequence getTextViewRight(){
        return textviewRight.getText();
    }
    public void setTextviewLeftColor(int color){
        textviewLeft.setTextColor(color);
    }
    public void setTextviewRightColor(int color){
        textviewRight.setTextColor(color);
    }
    public void setTextviewLeftTextSize(float textsize){
        textviewLeft.setTextSize(textsize);
    }
    public void setTextviewRightTextSize(float textsize){
        textviewRight.setTextSize(textsize);
    }
}

3.在布局文件中使用
運行在真機上如上圖所示

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="vcredit.com.customviewgroup.MainActivity">

    <vcredit.com.customviewgroup.ItemChooseView
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        app:textLeft="左邊一"
        app:textRight="右邊一" />
    <vcredit.com.customviewgroup.ItemChooseView
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        app:textLeft="左邊二"
        app:textRight="右邊二"
        app:textLeftColor="#f00"
        app:textRightColor="#0f0"/>
    <vcredit.com.customviewgroup.ItemChooseView
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        app:textLeft="左邊三"
        app:textRight="右邊三"
        app:textLeftTextSize="20sp"
        app:textRightTextSize="20sp"/>
</LinearLayout>
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容