android自定义控件(TopBar的实现)

很多时候,android提供的控件并不能满足实际开发的需要,而系统自然也提供了自定义控件的方法供开发者使用,下面就介绍一个简单的自定义控件TopBar(应用标题栏)的开发过程。

第一步:新建一个android项目

第二步:在res/values下新建一个attrs.xml的文件用来声明自定义控件的属性


    
      

    
      
    
     
       
        
      
        
        
      
        
        
      
      
        
      
      
        
      
        
        
      
        
        
      
      
        
      
      
        
      
      
        
      
      
        
      
      
    
     
       

    
    

第三步:也是最重要的一步,新建自定义控件类TopBar

在其中为TopBar的左右Button定义了接口,方便在后面使用过程中实现点击事件。

package com.topbar.test;

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

public class TopBar extends RelativeLayout {
	private Button leftButton;
	private Button rightButton;
	private TextView title;

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

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

	private String titleText;
	private float titleTextSize;
	private int titleTextColor;

	private LayoutParams leftParams, rightParams, titleParams;
	
	//实现自定义控件的点击事件
	public interface topBarOnclickListener{
		public void leftClick();
		public void rightClick();
	}

	private topBarOnclickListener listener;
	
	public void setTopBarOnclickListener(topBarOnclickListener listener){
		this.listener = listener;
	}
	
	@SuppressLint("NewApi")
	//在xml中要使用的自定义控件必须使用具有AttributesSet的构造方法
	public TopBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		//得到TopBar的declare-styleable
		TypedArray ta = context.obtainStyledAttributes(attrs,
				R.styleable.Topbar);
		//得到在xml中设置的属性值
		leftTextColor = ta.getColor(R.styleable.Topbar_leftTextColor, 0);
		leftBackground = ta.getDrawable(R.styleable.Topbar_leftBackground);
		leftText = ta.getString(R.styleable.Topbar_leftText);
		leftTextSize = ta.getDimension(R.styleable.Topbar_leftTextSize, 0);

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

		titleText = ta.getString(R.styleable.Topbar_titleText);
		titleTextColor = ta.getColor(R.styleable.Topbar_titleTextColor, 0);
		titleTextSize = ta.getDimension(R.styleable.Topbar_titleTextSize, 0);

		ta.recycle();
		
		leftButton = new Button(context);
		rightButton = new Button(context);
		title = new TextView(context);
		//为自定义控件中的控件设置属性
		leftButton.setText(leftText);
		leftButton.setTextColor(leftTextColor);
		leftButton.setTextSize(leftTextSize);
		leftButton.setBackground(leftBackground);

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

		title.setText(titleText);
		title.setTextColor(titleTextColor);
		title.setTextSize(titleTextSize);
		title.setGravity(Gravity.CENTER);

		setBackgroundColor(Color.GRAY);
		//为自定义控件中的控件设置布局属性
		leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);

		addView(leftButton, leftParams);

		rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);

		addView(rightButton, rightParams);

		titleParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		titleParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);

		addView(title, titleParams);
		
		leftButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				
				listener.leftClick();
			}
		});
		rightButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				listener.rightClick();
			}
		});
	}
}


第四步:在activity_main.xml中添加TopBar控件并设置属性


     
     
    
      
      
    
      
      

     
     


至此,TopBar自定义控件编写完成。看看效果图,虽然比较丑,但功能已经实现了




总结一下,自定义控件大致分为上面三个过程,其中较复杂的就是自定义控件类的定义,首先调用TypedArray来获得自定义的属性的值,这些值就是将来在布局文件中自定义控件的值,例如:

leftTextColor = ta.getColor(R.styleable.Topbar_leftTextColor, 0);

这行代码,R.styleable.Topbar_leftTextColor 是系统生成的默认的属性引用名,是用styleable名_属性名.

后面的0表示此属性的默认值,也就是在xml中定义时不设置此属性时它的取值。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值