自定义的控件里有ImageButton和普通的Button, ImageButton显示正常 但是普通的Button只能显示背景色 文字却无法显示 代码如下 求指教
public class DialpadAdditionalButtons extends FrameLayout { private static final String TAG = "DialpadAdditionalButtons"; private int mButtonWidth;
private int mButtonHeight;
private int mDividerHeight;
private int mDividerWidth; private Drawable mDividerVertical; private boolean mLayouted = false; public DialpadAdditionalButtons(Context context, AttributeSet attrs) {
super(context, attrs); Resources r = getResources();
/* Vanzo:tanjianhua on: Mon, 04 Mar 2013 23:15:17 +0800
mButtonWidth = r.getDimensionPixelSize(R.dimen.dialpad_additional_button_width);
*/
WindowManager mWindowManager = (WindowManager) context.getSystemService(
Context.WINDOW_SERVICE);
int width = mWindowManager.getDefaultDisplay().getWidth();
mButtonWidth = width / 3;
// End of Vanzo: tanjianhua
mButtonHeight = r.getDimensionPixelSize(R.dimen.dialpad_additional_button_height);
mDividerHeight = r.getDimensionPixelSize(R.dimen.dialpad_divider_height);
mDividerWidth = r.getDimensionPixelSize(R.dimen.dialpad_divider_width);
} @Override
protected void onFinishInflate() {
// TODO Auto-generated method stub
super.onFinishInflate(); init();
} protected void init() {
TypedArray typedArray = getContext().getTheme().obtainStyledAttributes(new int[] {
android.R.attr.selectableItemBackground
});
Drawable itemBackground = typedArray.getDrawable(0); typedArray = getContext().getTheme().obtainStyledAttributes(new int[] {android.R.attr.dividerVertical});
mDividerVertical = typedArray.getDrawable(0); ImageButton button = new ImageButton(getContext());
button.setImageResource(R.drawable.ic_dialpad_holo_dark);
button.setBackgroundDrawable(itemBackground);
button.setId(R.id.dialpadButton);
addView(button); View divider = new View(getContext());
divider.setBackgroundDrawable(mDividerVertical);
addView(divider);
//*****
Button btn = new Button(getContext());
//btn.setBackgroundDrawable(itemBackground.getConstantState().newDrawable());
btn.setId(R.id.filterAllButton);
btn.setTextColor(Color.WHITE);
btn.setBackgroundColor(Color.GREEN);
btn.setTextSize(25);
btn.setText("All");
addView(btn);
//btn.setVisibility(View.GONE); divider = new View(getContext());
divider.setBackgroundDrawable(mDividerVertical);
addView(divider);
Button btn2 = new Button(getContext());
//btn.setBackgroundDrawable(itemBackground.getConstantState().newDrawable());
btn2.setId(R.id.filterMissedButton);
btn2.setTextColor(Color.WHITE);
btn2.setBackgroundColor(Color.RED);
btn2.setTextSize(25);
btn2.setText("Missed");
addView(btn2);
//btn.setVisibility(View.GONE);
//*****
button = new ImageButton(getContext());
button.setBackgroundDrawable(itemBackground.getConstantState().newDrawable());
button.setId(R.id.deleteDigitsButton);
button.setImageResource(R.drawable.ic_dial_action_delete);
addView(button);
button = new ImageButton(getContext());
button.setImageResource(R.drawable.ic_dial_action_call);
//button.setBackgroundResource(R.drawable.btn_call);
button.setId(R.id.dialButton);
addView(button);
} protected void onLayout(boolean changed, int l, int t, int r, int b) {
if (mLayouted) {
return;
} mLayouted = true;
int left, top, right ,bottom; View child = getChildAt(0);
child.layout(0, 0, mButtonWidth, mButtonHeight); int dividerTop = (mButtonHeight - mDividerHeight) >> 1;
child = getChildAt(1);
left = mButtonWidth;
top = dividerTop;
right = mButtonWidth + mDividerWidth;
bottom = dividerTop + mDividerHeight;
child.layout(left, top, right ,bottom); child = getChildAt(2);
left = mButtonWidth;
top = 0;
right = mButtonWidth << 1;
bottom = mButtonHeight;
child.layout(left, top, right ,bottom);
((Button)child).setText("All"); child = getChildAt(3);
left = mButtonWidth << 1;
top = dividerTop;
right = (mButtonWidth << 1) + mDividerWidth;
bottom = dividerTop + mDividerHeight;
child.layout(left, top, right ,bottom); child = getChildAt(4);
left = mButtonWidth << 1;
top = 0;
right = (mButtonWidth << 1) + mButtonWidth;
bottom = mButtonHeight;
child.layout(left, top, right ,bottom);
((Button)child).setText("Missed");
//filter all buttons
child = getChildAt(5);
left = mButtonWidth;
top = 0;
right = mButtonWidth << 1;
bottom = mButtonHeight;
child.layout(left, top, right ,bottom);
//filter missed buttons
child = getChildAt(6);
left = mButtonWidth << 1;
top = 0;
right = (mButtonWidth << 1) + mButtonWidth;
bottom = mButtonHeight;
child.layout(left, top, right ,bottom);
}
}
控件android
public class DialpadAdditionalButtons extends FrameLayout { private static final String TAG = "DialpadAdditionalButtons"; private int mButtonWidth;
private int mButtonHeight;
private int mDividerHeight;
private int mDividerWidth; private Drawable mDividerVertical; private boolean mLayouted = false; public DialpadAdditionalButtons(Context context, AttributeSet attrs) {
super(context, attrs); Resources r = getResources();
/* Vanzo:tanjianhua on: Mon, 04 Mar 2013 23:15:17 +0800
mButtonWidth = r.getDimensionPixelSize(R.dimen.dialpad_additional_button_width);
*/
WindowManager mWindowManager = (WindowManager) context.getSystemService(
Context.WINDOW_SERVICE);
int width = mWindowManager.getDefaultDisplay().getWidth();
mButtonWidth = width / 3;
// End of Vanzo: tanjianhua
mButtonHeight = r.getDimensionPixelSize(R.dimen.dialpad_additional_button_height);
mDividerHeight = r.getDimensionPixelSize(R.dimen.dialpad_divider_height);
mDividerWidth = r.getDimensionPixelSize(R.dimen.dialpad_divider_width);
} @Override
protected void onFinishInflate() {
// TODO Auto-generated method stub
super.onFinishInflate(); init();
} protected void init() {
TypedArray typedArray = getContext().getTheme().obtainStyledAttributes(new int[] {
android.R.attr.selectableItemBackground
});
Drawable itemBackground = typedArray.getDrawable(0); typedArray = getContext().getTheme().obtainStyledAttributes(new int[] {android.R.attr.dividerVertical});
mDividerVertical = typedArray.getDrawable(0); ImageButton button = new ImageButton(getContext());
button.setImageResource(R.drawable.ic_dialpad_holo_dark);
button.setBackgroundDrawable(itemBackground);
button.setId(R.id.dialpadButton);
addView(button); View divider = new View(getContext());
divider.setBackgroundDrawable(mDividerVertical);
addView(divider);
//*****
Button btn = new Button(getContext());
//btn.setBackgroundDrawable(itemBackground.getConstantState().newDrawable());
btn.setId(R.id.filterAllButton);
btn.setTextColor(Color.WHITE);
btn.setBackgroundColor(Color.GREEN);
btn.setTextSize(25);
btn.setText("All");
addView(btn);
//btn.setVisibility(View.GONE); divider = new View(getContext());
divider.setBackgroundDrawable(mDividerVertical);
addView(divider);
Button btn2 = new Button(getContext());
//btn.setBackgroundDrawable(itemBackground.getConstantState().newDrawable());
btn2.setId(R.id.filterMissedButton);
btn2.setTextColor(Color.WHITE);
btn2.setBackgroundColor(Color.RED);
btn2.setTextSize(25);
btn2.setText("Missed");
addView(btn2);
//btn.setVisibility(View.GONE);
//*****
button = new ImageButton(getContext());
button.setBackgroundDrawable(itemBackground.getConstantState().newDrawable());
button.setId(R.id.deleteDigitsButton);
button.setImageResource(R.drawable.ic_dial_action_delete);
addView(button);
button = new ImageButton(getContext());
button.setImageResource(R.drawable.ic_dial_action_call);
//button.setBackgroundResource(R.drawable.btn_call);
button.setId(R.id.dialButton);
addView(button);
} protected void onLayout(boolean changed, int l, int t, int r, int b) {
if (mLayouted) {
return;
} mLayouted = true;
int left, top, right ,bottom; View child = getChildAt(0);
child.layout(0, 0, mButtonWidth, mButtonHeight); int dividerTop = (mButtonHeight - mDividerHeight) >> 1;
child = getChildAt(1);
left = mButtonWidth;
top = dividerTop;
right = mButtonWidth + mDividerWidth;
bottom = dividerTop + mDividerHeight;
child.layout(left, top, right ,bottom); child = getChildAt(2);
left = mButtonWidth;
top = 0;
right = mButtonWidth << 1;
bottom = mButtonHeight;
child.layout(left, top, right ,bottom);
((Button)child).setText("All"); child = getChildAt(3);
left = mButtonWidth << 1;
top = dividerTop;
right = (mButtonWidth << 1) + mDividerWidth;
bottom = dividerTop + mDividerHeight;
child.layout(left, top, right ,bottom); child = getChildAt(4);
left = mButtonWidth << 1;
top = 0;
right = (mButtonWidth << 1) + mButtonWidth;
bottom = mButtonHeight;
child.layout(left, top, right ,bottom);
((Button)child).setText("Missed");
//filter all buttons
child = getChildAt(5);
left = mButtonWidth;
top = 0;
right = mButtonWidth << 1;
bottom = mButtonHeight;
child.layout(left, top, right ,bottom);
//filter missed buttons
child = getChildAt(6);
left = mButtonWidth << 1;
top = 0;
right = (mButtonWidth << 1) + mButtonWidth;
bottom = mButtonHeight;
child.layout(left, top, right ,bottom);
}
}
控件android
解决方案 »
- Android下,调用linux的创建设备节点的命令mknod,没有任何反应。节点没有创建成功,但是也无出错信息。请问是怎么回事?
- 根据content://sms/inbox查询收件箱短信,模拟器可以,真机不行,为啥啊?
- android自定义摄像头程序,景物被拉伸
- weather应用crash问题
- android 在线网络电台收听
- 求一个等待对话框loading 具体说明前看内容
- 如何防止用户kill掉进程造成的异常?
- 关于android的学习方向问题?新人求解
- android spinner 文字不能居左
- eclipse android 求助
- 求助galaxy S3 一个问题
- android textView 自动滚动问题
btn2.setTextColor(Color.WHITE); 你换个黑色试试 白色估计是被背景颜色覆盖了