天天看點

Android開發之自定義View(二)

Android開發之自定義View(一) 中,講解了最複雜的一種自定義View,本次将剩下的兩種講完~~~ go,go,go

繼承原有的控件,在原有控件基礎上進行修改,如TextView,這種方式常見且簡單。以實作一個顯示粗體文本的TextView例子來講解。

1、自定義屬性

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="BoldTextView">
        <attr name="textIsBlod" format="boolean" />
    </declare-styleable>

</resources>
           

2、建立一個類繼承自TextView,很簡單,内容都是前面講過的

public class BoldTextView extends TextView
{

    private TextPaint paint;

    public BoldTextView(Context context, AttributeSet attrs)
    {
        super(context, attrs);

        TypedArray params = context.obtainStyledAttributes(attrs,
                R.styleable.BoldTextView);
        // 得到自定義控件的屬性值。
        boolean textIsBlod = params.getBoolean(
                R.styleable.BoldTextView_textIsBlod, false);
        setTextblod(textIsBlod);

        params.recycle();
    }

    // 設定粗體 其實就是将畫筆變粗即可
    public void setTextblod(boolean textblod)
    {
        if (textblod)
        {
            paint = super.getPaint();
            paint.setFakeBoldText(true);
        }
    }
}
           

3、布局

<com.example.diyview.BoldTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="粗體字,你能看出來嗎?"
        android:textSize="30sp"
        app:textIsBlod="true" />
           

4、運作測試

Android開發之自定義View(二)

自定義View4.png

重新拼裝組合,這種方式也比較常見。以實作一個圖檔文字按鈕例子來講解。
<declare-styleable name="ImgBtn">
        <attr name="imgbtn_title" format="string" />
        <attr name="imgbtn_icon" format="reference" />
 </declare-styleable>
           

2、組合控件布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="@dimen/activity_vertical_margin" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>
           

3、建立一個類繼承自LinearLayout

public class ImgBtn extends LinearLayout
{
    private TextView title;
    private ImageView icon;

    public ImgBtn(Context context)
    {
        super(context);

    }

    public ImgBtn(Context context, AttributeSet attrs)
    {
        super(context, attrs);

        // 加載布局
        LayoutInflater.from(context).inflate(R.layout.diy_view, this, true);

        // 找到控件
        title = (TextView) findViewById(R.id.title);
        icon = (ImageView) findViewById(R.id.icon);

        // 設定屬性
        TypedArray params = context.obtainStyledAttributes(attrs,
                R.styleable.ImgBtn);

        int resId = params.getResourceId(R.styleable.ImgBtn_imgbtn_icon,
                R.drawable.ic_launcher);
        // 設定圖表
        setIcon(resId);
        String title = params.getString(R.styleable.ImgBtn_imgbtn_title);
        // 設定文本
        setTitle(title);

        params.recycle();
    }

    public void setIcon(int resId)
    {
        icon.setImageResource(resId);

    }

    public void setTitle(String text)
    {

        title.setText(text);
    }

}


           

3、Activity布局

<com.example.diyview.ImgBtn
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:imgbtn_icon="@drawable/icon_set"
        app:imgbtn_title="設定" />
           
Android開發之自定義View(二)

自定義View5.png

繼續閱讀