两边的button不能点击,button的text属性和background属性设置后都能显示,但是点击上面的文字却没任何反应,就好像变成了textview一样。
package com.example.test;
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;
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;
public Topbar(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
TypedArray ta=context.obtainStyledAttributes(attrs,R.styleable.Topbar);
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);
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);
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(tvTitle, titleparams);
}
}