Android教你一步一步实现简单重用自定义控件

老规矩,先看图,没图说个JB
在这里插入图片描述
像这种的布局都是固定的样式,只是图片和文字改变,要是按照正常的写布局文件,要写好长,重用性很差。

1.先创建一个布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android">
        <ImageView
            android:id="@+id/iv_left"
            android:layout_height="52dp"
            android:layout_width="52dp"
            android:src="@drawable/ic_smart_ask"
            android:layout_centerVertical="true"
            />

        <LinearLayout
            android:id="@+id/ll_content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:gravity="center_vertical"
            android:orientation="vertical"
           android:layout_toRightOf="@id/iv_left"
            android:layout_marginLeft="@dimen/text_layout_18_dp"
            >
            <TextView
                android:id="@+id/tv_top"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="智能问诊"
                android:textColor="#ff333333"
                android:textSize="16sp"
                />
            <TextView
                android:id="@+id/tv_bottom"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="快速得到解决方案,避免盲目就医"
                android:textColor="@color/tv999"
                android:textSize="13sp"
                android:layout_marginTop="@dimen/text_layout_10_dp"
                />
        </LinearLayout>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_right"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            />
    </RelativeLayout>


2.自定义控件类

/**
 * 问诊自定义View
 * Created by Administrator on 2018/12/15.
 */

public class AskLayoutNormalView extends RelativeLayout {
    ImageView leftImage;
    TextView topText;
    TextView bottomText;
    public AskLayoutNormalView(Context context) {
        super(context);
    }

    public AskLayoutNormalView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.layout_ask_text_normal,this,true);

        leftImage = findViewById(R.id.iv_left);
        topText = findViewById(R.id.tv_top);
        bottomText = findViewById(R.id.tv_bottom);
        TypedArray attributes = context.obtainStyledAttributes(attrs,R.styleable.AskLayoutNormalView);
        if(attributes != null){
            int leftImg = attributes.getResourceId(R.styleable.AskLayoutNormalView_leftDrawable,-1);
            String topString = attributes.getString(R.styleable.AskLayoutNormalView_topText);
            String bottomString = attributes.getString(R.styleable.AskLayoutNormalView_bottomText);
            if(leftImg != -1){
                leftImage.setImageResource(leftImg);
            }
            topText.setText(topString);
            bottomText.setText(bottomString);
        }
    }

    public AskLayoutNormalView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


}

3.在attrs中定义标签

<!--问诊自定义View-->
    <declare-styleable name="AskLayoutNormalView">
        <attr name="leftDrawable" format="reference|integer"></attr>
        <attr name="topText" format="string"></attr>
        <attr name="bottomText" format="string"></attr>
    </declare-styleable>

4.在布局文件中使用

<com.jkgl.view.AskLayoutNormalView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:leftDrawable="@drawable/ic_smart_ask"
        app:topText="智能问诊"
        app:bottomText="快速得到解决方案,避免盲目就医"
        android:layout_marginLeft="@dimen/text_layout_15_dp"
        android:layout_marginRight="@dimen/text_layout_15_dp"
        android:layout_marginTop="@dimen/text_layout_10_dp"
        android:paddingLeft="@dimen/text_layout_15_dp"
        android:paddingRight="@dimen/text_layout_15_dp"
        android:paddingTop="@dimen/text_layout_30_dp"
        android:paddingBottom="@dimen/text_layout_30_dp"
        android:background="@color/white"
        >
    </com.jkgl.view.AskLayoutNormalView>

其中

app:leftDrawable="@drawable/ic_smart_ask"
app:topText="智能问诊"
app:bottomText="快速得到解决方案,避免盲目就医"

这三个标签就是填写数据的。

结束…
是不是很简单,还很实用~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值