当使用android:drawableLeft时,Android 4.2无法正确显示RadioButton。 drawableRight没问题。
左侧的drawable与Radio按钮图形重叠。并且至少Android 2.2-4.1似乎可以使用drawableLeft。
你知道解决方法吗?以编程方式设置drawableLeft无法解决此问题。
4.2 Android问题
4.1 Android正确渲染(至少Android 2.2-4.0也正确渲染)
Android XML布局代码:
<RadioButton
android:id="@+id/radio_cloud_dropbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:drawableLeft="@drawable/dropbox_logo"
android:checked="true"
android:text="@string/lbl_cloud_dropbox" />
答案 0 :(得分:6)
你可以在单选按钮上使用:
设置按钮@null,左边的drawable设置按钮:
<RadioGroup
android:id="@+id/hn_radio_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RadioButton
android:id="@+id/rb_Select_by_proposal"
style="@style/RadioButtonText"
android:checked="true"
android:tag="1"
android:text="@string/select_by_proposal" />
<RadioButton
android:id="@+id/rb_Select_by_first_letter"
style="@style/RadioButtonText"
android:tag="2"
android:text="@string/select_by_first_letter" />
</RadioGroup>
这样的风格:
<style name="RadioButtonText">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:button">@null</item>
<item name="android:drawableLeft">@drawable/radio_selector</item>
<item name="android:drawablePadding">10dp</item>
</style>
用于按钮和文本之间的空格:
<item name="android:drawablePadding">10dp</item>
答案 1 :(得分:0)
它可能是Android 4.2中添加到平台的新布局方向支持的产品(即支持从左到右或从右到左的布局)。这会影响他们如何放置左侧和右侧可绘制内容。您的android:targetSdkVersion
清单属性设置为什么?从技术上讲,我相信,如果设置为16或更低,默认情况下应禁用这些布局更改,即使您的区域设置会启用它们。
我建议在http://b.android.com提交错误,因为在RTL模式下,我希望按钮可绘制在另一边。与此同时,您可以尝试通过向layoutDirection
添加TextView
属性来强制布局方向,并强制它为LTR,使其看起来与您期望的一样。您也可以使用setLayoutDirection()
答案 2 :(得分:0)
我对此错误的临时解决方案是将RadioGroup
替换为TableLayout
布局,其中包含TableRow
和RadioButton
TextView
。即使还有其他解决方案。
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableRow android:onClick="onDropboxClick" >
<RadioButton
android:id="@+id/radio_cloud_dropbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:onClick="onDropboxClick" />
<TextView
android:drawableLeft="@drawable/cloud_logo_dropbox"
android:gravity="center_vertical"
android:text="Dropbox" />
</TableRow>
<TableRow android:onClick="onGoogleDriveClick" >
<RadioButton
android:id="@+id/radio_cloud_google_drive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:onClick="onGoogleDriveClick" />
<TextView
android:drawableLeft="@drawable/cloud_logo_google_drive"
android:gravity="center_vertical"
android:text="Google Drive" />
</TableRow>
</TableLayout>
但由于RadioGroup
无法与嵌套布局一起使用,因此必须在事件处理程序方法中的关联活动中手动完成选择/取消选中已检查状态:
public void onDropboxClick(View view) {
checkRadio(R.id.radio_cloud_dropbox);
}
public void onGoogleDriveClick(View view) {
checkRadio(R.id.radio_cloud_google_drive);
}
private void checkRadio(int radioId) {
if (radioId == R.id.radio_cloud_dropbox) {
dropboxRadio.setChecked(true);
googleDriveRadio.setChecked(false);
} else if (radioId == R.id.radio_cloud_google_drive) {
dropboxRadio.setChecked(false);
googleDriveRadio.setChecked(true);
}
}
答案 3 :(得分:0)
这似乎在Android 4.2.2中得到修复。