カスタム ビュー (複数のコンポーネントで構成されるレイアウト)

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. レイアウトの下に XML レイアウトを作成します (layout_search、ここでは便宜上直接 dp を使用し、レイヤーを 1 つ減らすためにマージを使用します)。

<?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. クラスを作成し、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);
    }
}

終わり...

おすすめ

転載: blog.csdn.net/qq_41873558/article/details/87879998