由于常规Button
类的边距和填充问题,我一直在研究Layout
扩展的自定义FrameLayout
。我遇到了一个问题,布局的内容没有显示出来。一旦我将其更改为另一个,例如RelativeLayout
内容就会显示出来。
这是我的相关代码:
private static final int COLOR = R.styleable.MyButton_color;
private static final int TEXT = R.styleable.MyButton_text;
public MyButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
LayoutParams backgroundParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
backgroundParams.gravity = Gravity.FILL;
background = new ImageView(context, null, defStyle);
addView(background, backgroundParams);
text = new TextView(context, null, defStyle);
text.setTypeface(null, Typeface.BOLD);
LayoutParams textParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
textParams.gravity = Gravity.CENTER;
addView(text, textParams);
TypedArray a = context.obtainStyledAttributes(R.styleable.MyButton);
try {
setColor(Color.values()[a.getInteger(COLOR, 0)]);
setText(a.getString(TEXT));
} finally {
a.recycle();
}
}
public void setText(CharSequence text) {
this.text.setText(text);
}
private void setColor(Color color) {
switch (color) {
case ORANGE:
text.setTextColor(getContext().getResources().getColor(R.color.text_blue));
background.setImageResource(R.drawable.button_orange);
break;
case BLUE:
//TODO set colors
break;
}
}
我在这个上缺少什么?