Android自定義View

自定義控件也就是自己來編寫控件,而非使用Android中提供的控件

在這種方法中,大概的步驟是這樣的

1.我們的自定義控件和其他的控件一樣,應該寫成一個類,而這個類的屬性是是有自己來決定的.

2.我們要在res/values目錄下建立一個attrs.xml的文件,並在此文件中增加對控件的屬性的定義.

3.使用AttributeSet來完成控件類的構造函數,並在構造函數中將自定義控件類中變量與attrs.xml中的屬性連接起來.

4.在自定義控件類中使用這些已經連接的屬性變量.

5.將自定義的控件類定義到佈局用的xml文件中去.

6.在界面中生成此自定義控件類對象,並加以使用.

創建一個atts.xml

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

    //我們自定義的屬性
    //declare-styleable是給自定義控件添加自定義屬性用的
    //在<resources>標籤下使用<declare-styleable name="Topbar">標籤來告訴框架它包含的屬性就是自定義控件Topbar中的屬性

    <declare-styleable name="Topbar">
        //定義屬性名字,所應用資源爲string
        <attr name="title" format="string"/>

        //dimension尺寸dp
        <attr name="titleTextSize" format="dimension"/>
        <attr name="titleTextColor" format="color"/>

        <attr name="leftTextColor" format="color"/>
        <attr name="leftBackground" format="reference|color"/>
        <attr name="leftText" format="string"/>

        <attr name="rightTextColor" format="color"/>
        <attr name="rightBackground" format="reference|color"/>
        <attr name="rightText" format="string"/>

    </declare-styleable>
</resources>

創建Topbar.java
在自定義控件中寫屬性,最後在控件方法中獲取這些值

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * Created by huang on 2016/8/21.
 */

//繼承RelativeLayout並重寫構造方法
public class Topbar extends RelativeLayout {


    private Button leftButton, rightButton;
    private TextView tvTitle;

    private int leftTextColor;
    private Drawable leftBackground;
    private String leftText;

    private int rightTextColor;
    private Drawable rightBackground;
    private String rightText;

    private float titletextSize;
    private int titleTextColor;
    private String title;


    private LayoutParams leftParams, rightParams,titleParams;

    //需要自定義屬性就需要添加一個參數AttributeSet
    public Topbar(Context context, AttributeSet attrs) {
        super(context, attrs);

        //利用TypedArray可以調用Topbar
        //使用AttributeSet來完成控件類的構造函數,並在構造函數中將自定義控件類中變量與attrs.xml中的屬性連接起來.
        TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.Topbar);

        //從typeArray獲取相應值,第二個參數爲默認值,如第一個參數在atts.xml中沒有定義,返回第二個參數值  
        leftTextColor = ta.getColor(R.styleable.Topbar_leftTextColor,0);
        leftBackground = ta.getDrawable(R.styleable.Topbar_leftBackground);
        leftText = ta.getString(R.styleable.Topbar_leftText);

        rightTextColor = ta.getColor(R.styleable.Topbar_rightTextColor,0);
        rightBackground = ta.getDrawable(R.styleable.Topbar_rightBackground);
        rightText = ta.getString(R.styleable.Topbar_rightText);

        titletextSize = ta.getDimension(R.styleable.Topbar_titleTextSize,0);
        titleTextColor = ta.getColor(R.styleable.Topbar_titleTextColor,0);
        title = ta.getString(R.styleable.Topbar_title);

        //處理完TypedArray賦值完,要記得回收ta,避免浪費也避免緩存出錯
        ta.recycle();

        //處理使用控件
        leftButton = new Button(context);
        rightButton = new Button(context);
        tvTitle = new TextView(context);

        leftButton.setTextColor(leftTextColor);
        leftButton.setBackground(leftBackground);
        leftButton.setText(leftText);


        rightButton.setTextColor(rightTextColor);
        rightButton.setBackground(rightBackground);
        rightButton.setText(rightText);

        tvTitle.setTextColor(titleTextColor);
        tvTitle.setTextSize(titletextSize);
        tvTitle.setText(title);
        tvTitle.setGravity(Gravity.CENTER);

        //
        setBackgroundColor(0xFFF59563);

        //
        leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        //添加規則
        leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);

        //把leftParams加入到leftButton
        addView(leftButton,leftParams);

        rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        //添加規則
        rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);

        //把leftParams加入到leftButton
        addView(rightButton,rightParams);

        titleParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        //添加規則
        titleParams.addRule(RelativeLayout.ALIGN_PARENT_TOP,TRUE);

        //把leftParams加入到leftButton
        addView(tvTitle,titleParams);

    }
}

在佈局文件中的寫法

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

//佈局文件中使用自定義屬性的時候,先引用xmlns
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

//直接寫包名
    <com.example.huang.title.Topbar
        android:id="@+id/topbar"
        android:layout_width="wrap_content"
        android:layout_height="50dp"

        custom:leftText="BACK"
        custom:leftTextColor="#FFFFFF"

        custom:rightText="MORE"
        custom:rightTextColor="#FFFFFF"

        custom:title="自定標題"
        custom:titleTextColor="#123412"
        custom:titleTextSize="10sp"/>

</RelativeLayout>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章