兩邊的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);
?
}
}
2016-08-05
同問,請問解決了嗎
2016-01-09
button沒有設置click事件