Android 使复选框大小相同
我的一个屏幕包含布局文件中预定义的复选框和以编程方式添加的复选框。问题是他们生产了不同的尺寸 布局文件中定义的布局最终显示一个更大的方形框;选中时,复选标记为绿色(看起来正常):Android 使复选框大小相同,android,checkbox,Android,Checkbox,我的一个屏幕包含布局文件中预定义的复选框和以编程方式添加的复选框。问题是他们生产了不同的尺寸 布局文件中定义的布局最终显示一个更大的方形框;选中时,复选标记为绿色(看起来正常): 如何修改代码,使所有复选框具有相同的外观?我建议您不要通过编程方式创建复选框,而是将其放大。可能要贵一点,但您可以从父视图组获得正确的LayoutParams,并且可以更轻松地设置膨胀复选框的样式(因为它位于xml布局中)。示例代码如下: “我的复选框”xml布局 <?xml version="1.0" enco
如何修改代码,使所有复选框具有相同的外观?我建议您不要通过编程方式创建复选框,而是将其放大。可能要贵一点,但您可以从父视图组获得正确的LayoutParams,并且可以更轻松地设置膨胀复选框的样式(因为它位于xml布局中)。示例代码如下: “我的复选框”xml布局
<?xml version="1.0" encoding="utf-8"?>
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
</CheckBox>
// excerpt from the activity layout
<LinearLayout
android:id="@+id/checkbox_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CB 1" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CB 2" />
</LinearLayout>
不要使用
包装内容
,而是使用匹配父项
。感谢您的回复,但仍然是一样的。您可以使用CheckBox CheckBox=new CheckBox(getApplicationContext(),您的属性,任何样式);
<?xml version="1.0" encoding="utf-8"?>
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
</CheckBox>
// excerpt from the activity layout
<LinearLayout
android:id="@+id/checkbox_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CB 1" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CB 2" />
</LinearLayout>
// Excerpt from the Activity
private TextView targetTextView;
private ViewGroup checkboxContainer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkboxContainer = (ViewGroup) findViewById(R.id.checkbox_container);
CheckBox checkbox3 = (CheckBox) getLayoutInflater().inflate(R.layout.my_checkbox, checkboxContainer, false);
checkbox3.setText("Inflated Checkbox");
checkboxContainer.addView(checkbox3);
}