自定义View(多个组件组成的layout)

虽然include也节省了写xml的时间,但是里面的响应事件还是每次都要写一遍,很多时候,为了节省开发时间,对于重复的布局,我们会直接通过自定义来实现。

1.在valuse文件下创建一个attrs文件():

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="SearchView">//自定义的class
        <attr name="textcolor" format="color"/>
        <attr name="textsize" format="dimension"/>
        <attr name="background" format="color"/>
    </declare-styleable>

    <declare-styleable name="TitleView">
        <attr name="textcolor" format="color"/>
        <attr name="textsize" format="dimension"/>
        <attr name="background" format="color"/>
    </declare-styleable>
</resources>

2.layout下创建一个xml布局(layout_search,这里为方便看直接dp,用merge是为了少一层):

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

    <ImageView
        android:id="@+id/market"
        android:layout_marginTop="8dp"
        android:layout_marginRight="10dp"
        android:padding="5dp"
        android:layout_alignParentRight="true"
        android:src="@drawable/ic_market"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/fast"
        android:padding="5dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="8dp"
        android:layout_toLeftOf="@+id/market"
        android:src="@drawable/ic_fast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


    <EditText
        android:id="@+id/h_input"
        android:layout_toLeftOf="@+id/fast"
        android:textSize="12sp"
        android:singleLine="true"
        android:textColorHint="@color/text_assistant3"
        android:textColor="@color/text_assistant3"
        android:hint="点击搜索"
        android:layout_alignBottom="@id/market"
        android:layout_alignTop="@id/market"
        android:layout_marginLeft="15dp"
        android:minHeight="32dp"
        android:paddingLeft="35dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:background="@drawable/bg_search"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/h_search"
        android:padding="8dp"
        android:layout_alignLeft="@+id/h_input"
        android:layout_alignTop="@+id/h_input"
        android:layout_alignBottom="@+id/h_input"
        android:src="@drawable/ic_search"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</merge>

3.创建一个class并继承View(LinearLayout,ViewGroup,RelativeLayout,TextView等都可以,我这里是继承RelativeLayout):

public class SearchView extends RelativeLayout {

    @BindView(R.id.market)
    ImageView market;
    @BindView(R.id.fast)
    ImageView fast;
    @BindView(R.id.h_input)
    EditText hInput;
    @BindView(R.id.h_search)
    ImageView hSearch;

    public SearchView(Context context) {
        super(context);
    }

    public SearchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        View view = LayoutInflater.from(context).inflate(R.layout.layout_search, this, true);
        ButterKnife.bind(this, view);//这里用的是黄油刀如果不是用小刀,就直接像Activity一样findId
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.SearchView);
        int textColor = array.getColor(R.styleable.SearchView_textcolor, 0XFF00FF00);
        if (array != null) {
            hInput.setTextColor(textColor);
        }
    }

    @OnClick({R.id.market, R.id.fast, R.id.h_input, R.id.h_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.market:
                TipsUtil.log("market");
                break;
            case R.id.fast:
                TipsUtil.log("fast");
                break;
            case R.id.h_search:
                TipsUtil.log("search");
                break;
            case R.id.h_input:
                TipsUtil.log("input");
                break;
        }
    }
}

/** 工具 **/

public class TipsUtil {

    //log
    public static void log(String text){
        Log.e("TAG","-------"+text);
    }
}

END...

猜你喜欢

转载自blog.csdn.net/qq_41873558/article/details/87879998