Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 安卓自定義控件——標題欄的復用

安卓自定義控件——標題欄的復用

編輯:關於Android編程

一貫作風,先看效果圖,再實現

\

編寫自定義屬性文件atts.xml,自定義屬性中涉及到的屬性有左右兩邊的button的背景圖,中間標題的內容,字體大小,字體顏色。



    
        
        
        
        
        
    


編寫布局文件layout_topbar.xml,使用相對布局,左邊一個button,跟父控件左對齊後外邊距為5dp,右邊的button也是一樣,中間的標題居中顯示



    

編寫自定義控件,繼承RelativeLayout,獲取自定義屬性並給對應的控件賦值

package cn.edu.zafu.view.topbar;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * Created by lizhangqu on 2015/1/18.
 */
public class TopBar extends RelativeLayout {
    private Button leftButton, rightButton;
    private TextView titleTextView;
    private OnLeftAndRightClickListener listener;//監聽點擊事件
    //設置監聽器
    public void setOnLeftAndRightClickListener(OnLeftAndRightClickListener listener) {
        this.listener = listener;
    }
    //設置左邊按鈕的可見性
    public void setLeftButtonVisibility(boolean flag){
        if(flag)
            leftButton.setVisibility(VISIBLE);
        else
            leftButton.setVisibility(INVISIBLE);
    }
    //設置右邊按鈕的可見性
    public void setRightButtonVisibility(boolean flag){
        if(flag)
            rightButton.setVisibility(VISIBLE);
        else
            rightButton.setVisibility(INVISIBLE);
    }
    //按鈕點擊接口
    public interface OnLeftAndRightClickListener {
        public void onLeftButtonClick();

        public void onRightButtonClick();
    }

    public TopBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.layout_topbar, this);
        leftButton = (Button) findViewById(R.id.leftButton);
        rightButton = (Button) findViewById(R.id.rightButton);
        titleTextView = (TextView) findViewById(R.id.titleTextView);
        leftButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null)
                    listener.onLeftButtonClick();//點擊回調

            }
        });
        rightButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null)
                    listener.onRightButtonClick();//點擊回調
            }
        });
        //獲得自定義屬性並賦值
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TopBar);
        int leftBtnBackground = typedArray.getResourceId(R.styleable.TopBar_leftBackground, 0);
        int rightBtnBackground = typedArray.getResourceId(R.styleable.TopBar_rightBackground, 0);
        String titleText = typedArray.getString(R.styleable.TopBar_titleText);
        float titleTextSize = typedArray.getDimension(R.styleable.TopBar_titleTextSize, 0);
        int titleTextColor = typedArray.getColor(R.styleable.TopBar_titleTextColor, 0x38ad5a);
        typedArray.recycle();//釋放資源

        leftButton.setBackgroundResource(leftBtnBackground);
        rightButton.setBackgroundResource(rightBtnBackground);
        titleTextView.setText(titleText);
        titleTextView.setTextSize(titleTextSize);
        titleTextView.setTextColor(titleTextColor);
    }
}


調用自定義控件




    





package cn.edu.zafu.view.topbar;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TopBar topBar= (TopBar) findViewById(R.id.topbar);
        topBar.setOnLeftAndRightClickListener(new TopBar.OnLeftAndRightClickListener() {
            @Override
            public void onLeftButtonClick() {
                Toast.makeText(getApplicationContext(),"left",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onRightButtonClick() {
                Toast.makeText(getApplicationContext(),"right",Toast.LENGTH_SHORT).show();
            }
        });
        topBar.setLeftButtonVisibility(false);
    }
}


前面布局中用到的兩個selector如下
編寫left_button_selector.xml


    
    


編寫right_button_selector.xml



    
    



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