提前感谢您的帮助。
我有一个我在方法调用中创建的复选框
public int myMethod(Context context, doThing) {
if(doThing){
this.checkBox = new CheckBox(context);
do some stuff...
return 1;
}else{
do something else...
return 0;
}
}
我想通过调用checkBox.setBoxSize(mySize)来更改复选框的大小(实际的框不是框和文本组合)。 setBoxSize不是本机的android方法。是否可以这样做,如果是,我该怎么做呢?
答案 0 :(得分:3)
如果要创建具有预定义宽度和高度的复选框,可以使用此方法:
this.checkBox = new CheckBox(context);
this.checkBox.setLayoutParams(new LinearLayout.LayoutParams(width, height)); // or if your parent is RelativeLayout use RelativeLayout.LayoutParams(width, height)
注意,您的宽度和高度以像素为单位。
如果您想更改现有的布局参数,您应该执行以下操作:
private void setBoxSize(CheckBox checkBox) {
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) checkBox.getLayoutParams(); // or RelativeLayout.LayoutParams
params.height = yourHeightInPixels;
params.width = yourWidthInPixels;
checkBox.setLayoutParams(params);
}
修改强> 在这种情况下,如果您想更改复选框按钮的大小,则只能使用其中一种方法
checkBox.setButtonDrawable(int resId);
checkBox.setButtonDrawable(Drawable d);
并设置适当尺寸的drawables。不幸的是,Android没有办法操纵按钮(正如你提到的那样 - "框")。
drawable的示例,即用作复选框的按钮:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="@drawable/ic_check_on" /> <!-- checked -->
<item android:state_checked="false"
android:drawable="@drawable/ic_check_off" />
</selector>
答案 1 :(得分:0)
您应该像这样设置checkbox
的布局参数
//width and height are in pixels
checkbox.setLayoutParams(new ViewGroup.LayoutParams(width,height));