自定义组合控件

这个控件可作为表单的item。可以自定义左右两边的文字颜色和大小。
先看效果

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>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容