android - 如何更改由 AlertDialog Builder setSingleChoiceItems 创建的 RadioButtons 的样式

标签 android android-layout

我正在创建一个 AlertDialog,它将显示一个单选按钮列表以供选择。它按照我的需要工作,除了创建它时,单选按钮显示在项目文本的左侧,我希望它们显示在对话框的最右侧

这是我得到的结果(没有足够的声誉来嵌入图像)http://img.ctrlv.in/img/15/09/29/560ab269bad4a.jpg

这是我的代码

AlertDialog.Builder builder = new AlertDialog.Builder(context);
String[] items = new String[]{"1997", "1998", "1999", "2000", "2001"}
builder.setSingleChoiceItems(items, 0, getItemSelectedCallback(false, items));
builder.create().show();

感谢您的帮助。

最佳答案

事实证明,这可以通过零重构并且不使用 Spinner 来实现。我要做的就是创建一个数组适配器,将项目转换为自定义布局。

ArrayAdapter<String> adapter = new ArrayAdapter<>(context, R.layout.checked_text_view, items);
builder.setSingleChoiceItems(adapter, 0, getItemSelectedCallback(false, items));

对于自定义布局,我使用了 CheckedTextView

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:checkMark="?android:attr/listChoiceIndicatorSingle"
/>

这样就完成了工作

关于android - 如何更改由 AlertDialog Builder setSingleChoiceItems 创建的 RadioButtons 的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32849697/

相关文章:

Android focusable 和 focusable in touch mode

android - 从 AttributeSet 获取 Android 风格的属性

java - 实现 Google Places API

android - Grabcut 中的断言错误

Android 之类的 Flutter 中的文本字段验证?

android - 卡片 View 未显示

android - 表格布局间距问题

android - 在方向更改时更改 fragment 布局

android - 命名空间 : how to know how and when to use them? 即 : xmlns:andorid, xmlns :app, xmlns:tools

java - 无法找到签名中引用的类(Landroid/sec/multiwindow/MultiWindow;)