Custom View (layout composed of multiple components)

Although include also saves the time of writing xml, the response events in it still need to be written every time. In many cases, in order to save development time, we will implement the repeated layout directly through customization.

1. Create an attrs file () under the valuse file:

<?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. Create an xml layout under layout (layout_search, here is direct dp for convenience, and merge is used to reduce one layer):

<?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. Create a class and inherit View (LinearLayout, ViewGroup, RelativeLayout, TextView, etc. are all available, here I inherit 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;
        }
    }
}

/** tool**/

public class TipsUtil {

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

END...

Guess you like

Origin blog.csdn.net/qq_41873558/article/details/87879998