屬性的設置與使用

爲了將某些公用的View抽取成通用的View,咱們須要用到自定義View,並且通常狀況下,爲了方便快捷,咱們須要在佈局文件中就設置好值,因此咱們須要學會運用屬性。接下來就讓咱們一塊兒進入實戰演練一番吧!java

案例:好比說咱們編寫一個ShopCheckItem類,繼承於RelativeLayout,用來做爲自定義的View,那麼咱們須要執行如下幾個步驟:android

編寫佈局文件

首先咱們須要編寫佈局文件,不要問爲何,自定義View的辦法有不少種,本文只講這種辦法,慢慢看你就懂了!bash

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

    <com.showjoy.view.SHIconFontTextView
        android:id="@+id/view_shop_check_item_icon"
        android:layout_width="32dp"
        android:layout_height="32dp"/>

    <TextView
        android:id="@+id/view_shop_check_item_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="5dp"
        android:layout_toRightOf="@id/view_shop_check_item_icon"
        android:textColor="@color/black"
        android:textSize="16sp" />

    <ImageView
        android:id="@+id/view_shop_check_item_selected"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:layout_alignParentRight="true"/>

</RelativeLayout>

自定義屬性

接着根據佈局,發現咱們須要這樣一些屬性,如:icon、name、selected。因而咱們就能夠在styles.xml文件中編寫自定義的屬性了,具體代碼以下所示:佈局

<declare-styleable name="ShopCheckItem">
    <attr name="shop_check_item_icon" format="string" />
    <attr name="shop_check_item_name" format="string" />
    <attr name="shop_check_item_name_color" format="color" />
    <attr name="shop_check_item_selected" format="boolean" />
</declare-styleable>

自定義View

編寫好屬性後,咱們就能夠開始自定義view的編寫了,通常包括如下幾個步驟:this

使用inflate方式將佈局轉換爲View

找到佈局文件中得控件即findViewById

獲取在佈局文件中自定義屬性的值

根據自定義屬性的值初始化控件

暴露接口

注意:接口不必定是interface,只要是提供給別人用的,就算是一個public方法也是接口,若是不清楚,能夠查看這篇文章:接口回調code

因此,完整代碼以下所示:orm

package com.showjoy.shop.common.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.showjoy.shop.R;
import com.showjoy.view.SHIconFontTextView;

/**
 * Created by qingfeng on 7/20/16.
 */
public class ShopCheckItem extends RelativeLayout {

    private SHIconFontTextView viewShopCheckItemIcon;
    private TextView viewShopCheckItemName;
    private ImageView viewShopCheckItemSelected;

    public ShopCheckItem(Context context) {
        super(context);
        init(context, null);
    }

    public ShopCheckItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public ShopCheckItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }


    private void init(Context context, AttributeSet attrs) {
        inflate(context, R.layout.view_pay_method, this);

        viewShopCheckItemIcon = (SHIconFontTextView) findViewById(R.id.view_shop_check_item_icon);
        viewShopCheckItemName = (TextView) findViewById(R.id.view_shop_check_item_name);
        viewShopCheckItemSelected = (ImageView) findViewById(R.id.view_shop_check_item_selected);

        if (null != attrs) {
            TypedArray typeArray = context.obtainStyledAttributes(attrs, R.styleable.ShopCheckItem);

            String icon = typeArray.getString(R.styleable.ShopCheckItem_shop_check_item_icon);
            if (!TextUtils.isEmpty(icon)) {
                viewShopCheckItemIcon.setText(icon);
            }
            String name = typeArray.getString(R.styleable.ShopCheckItem_shop_check_item_name);
            if (!TextUtils.isEmpty(name)) {
                viewShopCheckItemName.setText(name);
            }
            int color = typeArray.getColor(R.styleable.ShopCheckItem_shop_check_item_name_color,
                    context.getResources().getColor(R.color.grey5));
            viewShopCheckItemName.setTextColor(color);
            boolean selected = typeArray.getBoolean(R.styleable.ShopCheckItem_shop_check_item_selected, false);
            if (selected) {
                viewShopCheckItemSelected.setImageResource(R.mipmap.view_shop_check_item_selected);
            } else {
                viewShopCheckItemSelected.setImageResource(R.drawable.view_shop_check_item_unselected);
            }
            typeArray.recycle();
        }
    }

    public void setSelected(boolean selected) {
        if (selected) {
            viewShopCheckItemIcon.setTextColor(getContext().getResources().getColor(R.color.black));
            viewShopCheckItemName.setTextColor(getContext().getResources().getColor(R.color.black));
            viewShopCheckItemSelected.setImageResource(R.mipmap.view_shop_check_item_selected);
        } else {
            viewShopCheckItemIcon.setTextColor(getContext().getResources().getColor(R.color.grey5));
            viewShopCheckItemName.setTextColor(getContext().getResources().getColor(R.color.grey5));
            viewShopCheckItemSelected.setImageResource(R.drawable.view_shop_check_item_unselected);
        }
    }

}

總結

是否是很簡單,自定義View就是這麼簡單,固然自定義View不止這麼一種方式咯,你們能夠自行探索,而後在下方評論區域告訴我,蟹蟹~?xml

相關文章
相關標籤/搜索