美文网首页
自定义组合控件

自定义组合控件

作者: shada | 来源:发表于2017-04-07 11:37 被阅读22次

这个控件可作为表单的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>

相关文章

  • Android入门06 -- 自定义控件

    自定义组合控件 将几个子控件组合在一起,形成一个可复用的新的组合控件,自定义组合控件一般继承自RelativeLa...

  • 【Android】自定义控件

    Android自定义控件可以有三种实现方式 组合原生控件自己绘制控件继承原生控件 1 组合原生控件 1.1 组合原...

  • android2019-01-03

    1.View的绘制流程自定义控件:1、组合控件。这种自定义控件不需要我们自己绘制,而是使用原生控件组合成的新控件。...

  • 11-自定义组合控件以及使用

    一、自定义组合控件介绍 开发中,为了使用的方便,经常把一些控件组合成一个控件,那样就成为了我们的自定义组合控件,严...

  • Android组合控件详解 & 自定义属性

    组合控件详解 & 自定义属性 摘录来源:极客学院WiKi 组合控件是自定义控件的一种,只不过它是由其他几个原生控件...

  • 组合控件2——海贼王选项菜单

    之前的自定义控件——初识自定义控件,我们了解到了自定义控件分为三种,自制控件,组合控件,拓展控件。而我们在自制控件...

  • 自制控件3——仿qq侧滑删除

    在自定义控件——初识自定义控件里面,我们已经对自定义控件进行描述和分类。其分类分别是 自制控件 组合控件 拓展控件...

  • 自制控件2 —— 自制控件 仿qq侧滑菜单

    在自定义控件——初识自定义控件里面,我们已经对自定义控件进行描述和分类。其分类分别是 自制控件 组合控件 拓展控件...

  • 每天五道Android面试题,轻松进大厂2018-12-20

    一、View的绘制流程 自定义控件: 1、组合控件。这种自定义控件不需要我们自己绘制,而是使用原生控件组合成的新控...

  • Android面试复习-View

    自定义控件 1.组合控件。这种自定义控件不需要我们自己绘制,而是使用原生控件组合成新控件,如标题栏。 2.继承原有...

网友评论

      本文标题:自定义组合控件

      本文链接:https://www.haomeiwen.com/subject/mugcattx.html