Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> [安卓開發] 自定義View之一: 組合View

[安卓開發] 自定義View之一: 組合View

編輯:關於Android編程

一、自定義View的分類

1、組合View
2、繼承重寫View
3、全寫View

二、簡介

組合View,就是組合一些View來形成一個新的View。例如QQ的頭部欄

這裡寫圖片描述
這裡寫圖片描述

三、了解

**

android:layout_width="match_parent" 

上面這個前綴android是根據 根控件的

xmlns:**android**="http://schemas.android.com/apk/res/android"

這個來命名的,這是系統的。我們要自定義View的屬性,就加這個屬性

xmlns:app="http://schemas.android.com/apk/res-auto"

app就是我們定義的前綴名,當然你可以隨便修改。

實現類裡面通過下面的代碼來獲取到自定義的屬性,

TypedArray ta = mContext.obtainStyledAttributes(attrs,R.styleable.TopBar);

四、實現

1、atts.xml , 自定義View 的屬性 ,在values下新建文件atts.xml



    
    

        
        
        
        

        
        
        
        

        
        
        
        

        
        
        
        
    

2、新建TopBar.java ,自定義view的內容類

public class TopBar extends LinearLayout {

    //控件
    private Button btHeadLeft;
    private TextView tvHeadTitle;
    private Button btHeadRight;



    //左邊的Button
    private String leftText;
    private Drawable leftBack;
    private int leftTextColor;
    private float leftTextSize;

    //右邊的Button
    private String rightText;
    private Drawable rightBack;
    private int rightTextColor;
    private float rightTextSize;

    //中間的標題
    private String titleText;
    private Drawable titleBack;
    private int titleTextColor;
    private float titleTextSize;

    private Context mContext;

    private LayoutParams leftParams,rightParams,titleParams;

    public TopBar(Context context) {
        super(context);
    }


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

        this.mContext = context;

        initView(attrs);

        initListener();
    }


    /**
     * 初始化View
     * @param attrs atts文件
     */
    private void initView(AttributeSet attrs){
        //獲取自定義View的值
        TypedArray ta = mContext.obtainStyledAttributes(attrs,R.styleable.TopBar);

        rightText = ta.getString(R.styleable.TopBar_rightText);
        rightBack = ta.getDrawable(R.styleable.TopBar_rightBack);
        rightTextColor = ta.getColor(R.styleable.TopBar_rightTextColor,0);
        rightTextSize = ta.getDimension(R.styleable.TopBar_rightTextSize,0);

        leftText = ta.getString(R.styleable.TopBar_leftText);
        leftBack = ta.getDrawable(R.styleable.TopBar_leftBack);
        leftTextColor = ta.getColor(R.styleable.TopBar_leftTextColor,0);
        leftTextSize = ta.getDimension(R.styleable.TopBar_leftTextSize,0);

        titleText = ta.getString(R.styleable.TopBar_title);
        titleBack = ta.getDrawable(R.styleable.TopBar_titleBack);
        titleTextColor = ta.getColor(R.styleable.TopBar_titleTextColor,0);
        titleTextSize = ta.getDimension(R.styleable.TopBar_titleTextSize,0);

        ta.recycle();  //回收

        //初始化控件
        btHeadLeft = new Button(mContext);
        tvHeadTitle = new TextView(mContext);
        btHeadRight = new Button(mContext);


        //為控件設置值
        btHeadLeft.setText(leftText);
        btHeadLeft.setTextColor(leftTextColor);
        btHeadLeft.setTextSize(leftTextSize);
        btHeadLeft.setBackground(leftBack);

        btHeadRight.setText(rightText);
        btHeadRight.setTextColor(rightTextColor);
        btHeadRight.setTextSize(rightTextSize);
        btHeadRight.setBackground(rightBack);

        tvHeadTitle.setText(titleText);
        tvHeadTitle.setTextColor(titleTextColor);
        tvHeadTitle.setTextSize(titleTextSize);
        tvHeadTitle.setBackground(titleBack);

    //設置View的背景顏色
        this.setBackgroundColor(0xFF593363);

    //設置布局屬性
        leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        titleParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        titleParams.weight = 1;   
        tvHeadTitle.setGravity(Gravity.CENTER);

        this.addView(btHeadLeft,leftParams);
        this.addView(tvHeadTitle,titleParams);
        this.addView(btHeadRight,rightParams);
    }

    /**
     * 初始化回調事件
     */
    private void initListener(){
        btHeadLeft.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                iTopBarOnClicklistener.leftOnClick();
            }
        });

        btHeadRight.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                iTopBarOnClicklistener.rightOnClick();
            }
        });

        tvHeadTitle.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                iTopBarOnClicklistener.titleOnClick();
            }
        });
    }


    public Button getBtHeadLeft() {
        return btHeadLeft;
    }

    public void setBtHeadLeft(Button btHeadLeft) {
        this.btHeadLeft = btHeadLeft;
    }

    public TextView getTvHeadTitle() {
        return tvHeadTitle;
    }

    public void setTvHeadTitle(TextView tvHeadTitle) {
        this.tvHeadTitle = tvHeadTitle;
    }

    public Button getBtHeadRight() {
        return btHeadRight;
    }

    public void setBtHeadRight(Button btHeadRight) {
        this.btHeadRight = btHeadRight;
    }

    public String getLeftText() {
        return leftText;
    }

    public void setLeftText(String leftText) {
        this.leftText = leftText;
    }

    public Drawable getLeftBack() {
        return leftBack;
    }

    public void setLeftBack(Drawable leftBack) {
        this.leftBack = leftBack;
    }

    public int getLeftTextColor() {
        return leftTextColor;
    }

    public void setLeftTextColor(int leftTextColor) {
        this.leftTextColor = leftTextColor;
    }

    public float getLeftTextSize() {
        return leftTextSize;
    }

    public void setLeftTextSize(float leftTextSize) {
        this.leftTextSize = leftTextSize;
    }

    public String getRightText() {
        return rightText;
    }

    public void setRightText(String rightText) {
        this.rightText = rightText;
    }

    public Drawable getRightBack() {
        return rightBack;
    }

    public void setRightBack(Drawable rightBack) {
        this.rightBack = rightBack;
    }

    public int getRightTextColor() {
        return rightTextColor;
    }

    public void setRightTextColor(int rightTextColor) {
        this.rightTextColor = rightTextColor;
    }

    public float getRightTextSize() {
        return rightTextSize;
    }

    public void setRightTextSize(float rightTextSize) {
        this.rightTextSize = rightTextSize;
    }

    public String getTitleText() {
        return titleText;
    }

    public void setTitleText(String titleText) {
        this.titleText = titleText;
    }

    public Drawable getTitleBack() {
        return titleBack;
    }

    public void setTitleBack(Drawable titleBack) {
        this.titleBack = titleBack;
    }

    public int getTitleTextColor() {
        return titleTextColor;
    }

    public void setTitleTextColor(int titleTextColor) {
        this.titleTextColor = titleTextColor;
    }

    public float getTitleTextSize() {
        return titleTextSize;
    }

    public void setTitleTextSize(float titleTextSize) {
        this.titleTextSize = titleTextSize;
    }

    public Context getmContext() {
        return mContext;
    }

    public void setmContext(Context mContext) {
        this.mContext = mContext;
    }

    public LayoutParams getLeftParams() {
        return leftParams;
    }

    public void setLeftParams(LayoutParams leftParams) {
        this.leftParams = leftParams;
    }

    public LayoutParams getRightParams() {
        return rightParams;
    }

    public void setRightParams(LayoutParams rightParams) {
        this.rightParams = rightParams;
    }

    public LayoutParams getTitleParams() {
        return titleParams;
    }

    public void setTitleParams(LayoutParams titleParams) {
        this.titleParams = titleParams;
    }


    /**
     * 顯示隱藏左邊的View
     * @param vis View.VISIABLE ...
     */
    public void setLeftVisibility(int vis){

        btHeadLeft.setVisibility(vis);

    }

    /**
     * 顯示隱藏右邊的View
     * @param vis View.VISIABLE ...
     */
    public void setRightVisibility(int vis){

        btHeadRight.setVisibility(vis);

    }

    //點擊的回調接口
    public void setiTopBarOnClicklistener(ITopBarOnClicklistener iTopBarOnClicklistener) {
        this.iTopBarOnClicklistener = iTopBarOnClicklistener;
    }

    private ITopBarOnClicklistener iTopBarOnClicklistener;

    public interface ITopBarOnClicklistener{
        void leftOnClick();
        void titleOnClick();
        void rightOnClick();
    }

    public void setTopBarOnClickListener(ITopBarOnClicklistener iTopBarOnClicklistener){
        this.iTopBarOnClicklistener = iTopBarOnClicklistener;
    }

}

MainActivty.java

public class MainActivity extends AppCompatActivity implements TopBar.ITopBarOnClicklistener{

    private TopBar tbHeader;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tbHeader = (TopBar) findViewById(R.id.tb_header);

        tbHeader.setiTopBarOnClicklistener(this);

        tbHeader.setLeftVisibility(View.GONE);
    }

    @Override
    public void leftOnClick() {
        Toast.makeText(this,"left",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void titleOnClick() {
        Toast.makeText(this,"title",Toast.LENGTH_SHORT).show();

    }

    @Override
    public void rightOnClick() {
        Toast.makeText(this,"right",Toast.LENGTH_SHORT).show();

    }
}

activity_main.xml




    
    

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved