《Android群英传》Three自定义View,创建TopBar控件

1、自定义View属性。

 <?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="TopBar">
        <attr name="title1" format="string" />
        <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>

<!--在res资源目录下为自定义View写属性。通过 <declare-styleable>声明使用自定义属性,通过name="TopBar"确定引用的名字。
通过<attr>标签声明自己具体的自定义的属性是哪些,这里自定义了标题内容,标题字体大小,标题字的颜色,左字的颜色,背景等,并通过format指定属性
的类型,有些属性可以是颜色属性,也可以是引用属性,如按钮的背景可以指定为具体的颜色,也可以指定为一张图片用"|"来分割不同的属性reference|color -->

2、通过<include>标签引用View模版

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp"
    tools:context=".TopBarTest">
   <include layout="@layout/topbar" />
  
</RelativeLayout>

3、创建自定义控件。

package sunny.example.ahthreeview;
/*创建自定义控件TopBar,继承自ViewGroup-->RelativeLayout*/

import android.annotation.SuppressLint;

import android.content.Context;

import android.util.AttributeSet;

import android.widget.RelativeLayout;

import android.content.res.TypedArray;

import android.graphics.drawable.Drawable;

import android.widget.Button;

import android.widget.TextView;

import android.view.Gravity;

import android.view.View;
@SuppressLint("NewApi")
public class TopBar extends RelativeLayout{
 private int mLeftTextColor;
 private String mLeftText;
 private Drawable mLeftBackground;
 
 private int mRightTextColor;
 private String mRightText;
 private Drawable mRightBackground;
 
 private int titleTextColor;
 private String titleText;
 private int titleTextSize;
 
 private Button mLeftButton;
 private Button mRightButton;
 private TextView mTitleView;
 
 private LayoutParams mLeftParams;
 private LayoutParams mRightParams;
 private LayoutParams mTitleParams;
 
 private topBarClickListener mListener;
 
 public TopBar(Context context) {
  super(context);
  // TODO Auto-generated constructor stub
 }
 public TopBar(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  // TODO Auto-generated constructor stub
 }
 
 @SuppressLint("NewApi")
 public TopBar(Context context, AttributeSet attrs) {
  super(context, attrs);
  // TODO Auto-generated constructor stub
   setBackgroundColor(0xFFF59563);
   
   //在构造方法中以此获取在XML文件中自定义的那些属性
   //TypedArray android.content.Context.obtainStyledAttributes(AttributeSet set, int[] attrs)
   //Return a StyledAttributes holding the attribute values in set that are listed in attrs. 
  
   /*android.content.res.TypedArray
  Container for an array of values that were retrieved with Resources.
  Theme.obtainStyledAttributes(AttributeSet, int[], int, int) or 
  Resources.obtainAttributes. Be sure to call recycle when done with them. 
  The indices used to retrieve values from this structure correspond to the 
  positions of the attributes given to obtainStyledAttributes.
*/
  
   TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.TopBar);
   
   //Retrieve the color value for the attribute at index.
  //defValue: 返回码Value to return if the attribute is not defined or not a resource.
  
   mLeftTextColor = ta.getColor(R.styleable.TopBar_leftTextColor, 0);
   mLeftText = ta.getString(R.styleable.TopBar_leftText);
   mLeftBackground = ta.getDrawable(R.styleable.TopBar_leftBackground);
   
   mRightTextColor = ta.getColor(R.styleable.TopBar_rightTextColor, 0);
   mRightText = ta.getString(R.styleable.TopBar_rightText);
   mRightBackground = ta.getDrawable(R.styleable.TopBar_rightBackground);
   
   titleTextColor = ta.getColor(R.styleable.TopBar_titleTextColor, 0);
   titleTextSize = (int) ta.getDimension(R.styleable.TopBar_titleTextSize,10);
   titleText = ta.getString(R.styleable.TopBar_title1);
  
   //获取完TypedArray的值后需要调用TypedArray的recyle()方法完成资源回收
  
   ta.recycle();
   
   mLeftButton = new Button(context);
   mRightButton = new Button(context);
   mTitleView = new TextView(context);
   
   //为创建的组件元素加上自定义的属性值
  
   mLeftButton.setTextColor(mLeftTextColor);
   mLeftButton.setBackground(mLeftBackground);
   mLeftButton.setText(mLeftText);
   
   mRightButton.setTextColor(mRightTextColor);
   mRightButton.setBackground(mRightBackground);
   mRightButton.setText(mRightText);
   
   mTitleView.setTextColor(titleTextColor);
   mTitleView.setText(titleText);
   mTitleView.setTextSize(titleTextSize);
   mTitleView.setGravity(Gravity.CENTER);
   
   //为创建的组件元素设置布局
   
    mLeftParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
    mLeftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
    addView(mLeftButton,mLeftParams);
    
    mRightParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
    mRightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
    addView(mRightButton,mRightParams);
    
    mTitleParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
    mTitleParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);
    addView(mTitleView,mTitleParams);
    
  //不能在此构造方法中定义接口 public interface,接口要定义在top_level class中。因此定义在此方法外面
    
    //暴露接口给调用者(TopBarTest),按钮的点击事件不需要实现,//只需要调用接口方法,在调用者TopBarTest中实现
   
    mRightButton.setOnClickListener(new OnClickListener(){
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    mListener.rightClick();
   }
     
    });
    
    mLeftButton.setOnClickListener(new OnClickListener(){
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    mListener.leftClick();
   }
     
    });
 }

 //暴露一个方法给调用者TopBatTest来注册接口回调

 public void setOnTopbarClickListener(topBarClickListener mListener){
  this.mListener = mListener;
 }
 
 //定义接口

 public interface topBarClickListener{
  void leftClick();
  void rightClick();
 }
 
 public void setButtonVisible(int id,boolean flag){
  if(flag){
   if(id==0){
    mLeftButton.setVisibility(View.VISIBLE);
   }
   else{
    mRightButton.setVisibility(View.VISIBLE);
   }
  }
  else{
   if(id==0){ 
    mLeftButton.setVisibility(View.GONE);
   }
   else{
    mRightButton.setVisibility(View.GONE);
   }
  }
 }
 
}

4、调用者

 package sunny.example.ahthreeview;

import sunny.example.ahthreeview.TopBar.topBarClickListener;

import android.os.Bundle;

import android.support.v7.app.ActionBarActivity;

import android.widget.Toast;

public class TopBarTest extends ActionBarActivity{
  private TopBar mTopbar;
 @Override
 public void onCreate(Bundle savedInstanceState){
  super.onCreate(savedInstanceState);
  setContentView(R.layout.topbar_test);
  
  mTopbar = (TopBar)findViewById(R.id.topBar);
  
  //实现接口回调即实现接口中的方法
  
  mTopbar.setOnTopbarClickListener(new topBarClickListener(){
   @Override
   public void leftClick() {
    // TODO Auto-generated method stub
    Toast.makeText(TopBarTest.this, "rightClick", Toast.LENGTH_SHORT).show();
   }
   @Override
   public void rightClick() {
    // TODO Auto-generated method stub
    Toast.makeText(TopBarTest.this, "leftClick", Toast.LENGTH_SHORT).show();
   }
   
  });
 }
}

《Android群英传》Three自定义View,创建TopBar控件_第1张图片

你可能感兴趣的:(《Android群英传》Three自定义View,创建TopBar控件)