在 AlertDialog 中设置单选按钮和文本的样式

2024-03-28

I want to show a radio list inside an AlertDialog with custom styling, something like this.

因此,我创建了一个自定义主题并将其作为参数提供给 AlertDialog.Builder 的构造函数。

这是显示对话框的代码:

private void showSortDialog() {
final CharSequence[] options = new String[] {"Relevance", "Newest"};
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivityReference(),
                                                          R.style.RadioDialogTheme);
    builder.setTitle("Sort by");
    builder.setSingleChoiceItems(options, -1, new DialogInterface.OnClickListener() {
    . . . . 
    builder.create().show();
}

这是风格:

<style name="RadioDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:textColorAlertDialogListItem">@drawable/radiobutton_textcolor_selector
    </item>
    <item name="android:listChoiceIndicatorSingle">@drawable/apptheme_btn_radio_holo_light
    </item>
</style>

我能够找到一些在我的样式中添加的属性,以根据单选按钮/文本的状态更改其颜色。但我无法自定义文本外观(我想更改大小、提供填充等)。

我确信有一些属性可以用来设置文本样式,但我找不到它。谁能帮我解决这个问题吗?谢谢。


我想这几乎是你需要的:

fun showSortDialog(context: Activity) {
    val options = arrayOf(
        "Relevance",
        "Price - Low to High",
        "Price - High to Low",
        "Newest"
    )
    val builder = AlertDialog.Builder(context, R.style.MultiChoiceAlertDialog)
    val view = context.layoutInflater.inflate(R.layout.dialog_custom, null, false)
    val radioGroup = view.findViewById<RadioGroup>(R.id.radiogroup)

    val purpleColor = ContextCompat.getColor(context, R.color.purple)
    val radioStyle = ContextThemeWrapper(radioGroup.context, R.style.MyRadioButton)
    for (option in options) {
        val radioButton = RadioButton(radioStyle)
        radioButton.setText(option)
        radioGroup.addView(radioButton)
    }
    radioGroup.setOnCheckedChangeListener { group, checkedId ->
        for (child in radioGroup.children) {
            child as RadioButton
            if (child.id == checkedId) {
                child.setTextColor(purpleColor)
            } else {
                child.setTextColor(Color.BLACK)
            }
        }
    }
    builder.setView(view)
    builder.show()
}

styles:

 <style name="YourAlertDialog.Button" parent="Widget.MaterialComponents.Button.TextButton">
    <item name="android:textColor">@color/colorPrimary</item>
    <item name="android:textSize">20sp</item>
    <item name="android:textAllCaps">false</item>
    <item name="android:gravity">left</item>
    <item name="android:letterSpacing">0</item>
</style>
<style name="MultiChoiceAlertDialog" parent="Theme.MaterialComponents.Light.Dialog.Alert">
    <item name="buttonBarPositiveButtonStyle">@style/YourAlertDialog.Button</item>
    <item name="buttonBarNegativeButtonStyle">@style/YourAlertDialog.Button</item>
    <item name="buttonBarNeutralButtonStyle">@style/YourAlertDialog.Button</item>
    <item name="android:background">#fff</item>
    <item name="android:textColorPrimary">#000</item>
    <item name="android:textColor">@drawable/selector_custom</item>
    <item name="android:colorActivatedHighlight">#0f0</item>
    <item name="android:colorControlActivated">#00f</item>
    <item name="colorControlNormal">@color/gray</item>
    <item name="colorControlActivated">@color/purple</item>
    <item name="dialogCornerRadius">8dp</item>
</style>

<style name="MyRadioButton" parent="Theme.AppCompat.Light">
    <item name="colorControlNormal">@color/gray</item>
    <item name="colorControlActivated">@color/purple</item>
</style>

和自定义布局(dialog_custom.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.AppCompatTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:text="Sort by"
    android:textColor="#000"
    android:textSize="20sp" />
<RadioGroup
    android:id="@+id/radiogroup"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
</RadioGroup>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 AlertDialog 中设置单选按钮和文本的样式 的相关文章

  • RxAndroid - 单击时重试可观察

    我在 Android 应用程序中使用 rxAndroid 和 rxKotlin 来异步处理网络请求 现在我想仅在单击 Snackbar 按钮后重试失败的网络请求 我现在的代码 val citiesService ApiFactory cit
  • 如何获取 Android 应用程序的内部版本号?

    我需要弄清楚如何获取或创建我的 Android 应用程序的内部版本号 我需要在用户界面中显示内部版本号 我必须做点什么吗AndroidManifest xml 如果您使用 Gradle 插件 Android Studio 从版本 0 7 0
  • android studio 和 android SDK 捆绑的 eclipse 版本有什么区别

    我没有 Android 开发经验 我想开始编写应用程序 The 官方开发者工具页面 http developer android com tools index html包含两个不同 IDE 的链接 第一个包含捆绑的 ADT 版本Eclip
  • 在 Android 中使用 Facebook Achievement API

    我知道这可能看起来像一个通用问题 但找到有关该主题的信息似乎非常困难 因此 如果某个地方存在完整的示例 指南 源代码链接 我将不胜感激 我正在开发一款 Android 游戏 希望集成 Facebook 成就 我想要的只是在用户完成某个谜题时
  • 在操作栏中编辑文本

    我正在使用 Action Bar Sherlock 为我的应用程序创建 UI 在将依赖项添加到我的项目等后 我创建了一个活动来测试它 public class PPS extends SherlockActivity Override pu
  • SeekBar setProgress 丢弃次要进度

    我的小学和中学的进步是同时增长的 中学的进步比小学的进步快 每次我更新主要进度时 次要进度都会丢失 就像它为零或小于主要进度一样 这会产生令人讨厌的闪烁 我发现的唯一解决方法是在设置主数据库后将辅助数据库设置为自身 我添加了 1 因为 Se
  • 如何更改所有 ListView 的默认分隔线颜色

    我正在尝试为 style xml 中的所有 listView 应用默认样式 请注意 在某些地方我使用嵌套列表视图 In 样式 xml
  • 使用Web蓝牙API时找不到移动设备

    我正在学习 Web 蓝牙 API 使用 google 开发控制台 我无法找到我的移动设备 还尝试了 github 上提供的演示 https github com WebBluetoothCG demos https github com W
  • ListView 中的焦点控件

    上下文 我想要一个不会获得焦点的 ListView 例如不会 当用户触摸它时突出显示该行 然而每个行小部件都有它自己的 OnClickListener 这是我在布局 xml 中指定的内容 android choiceMode none an
  • 带操作栏的 requestFeature

    所以我正在查看 google 的操作栏 api 演示 他们有这个 The Action Bar is a window feature The feature must be requested before setting a conte
  • 如何通过代码改变Android SlidingDrawer的方向?

    当我从横向模式更改为纵向模式时 我无法找到设置 SlidingDrawer 方向的方法 反之亦然 最初我将 xml 的方向设置为垂直 当手机处于横向模式时 我需要将方向更改为水平 因此我将手柄放在左侧 有人有什么想法吗 我认为按照标准这是不
  • 如何防止机器人程序和垃圾邮件 API 请求?

    我正在使用react native 开发一个Android 应用程序 该应用程序与我正在为该应用程序开发的API 进行通信 该 API 是使用 Laravel 和 Laravel Passport 构建的 我知道 Android 应用程序可
  • 如何从 Java 类调用 Kotlin 类

    我需要将意图从 java 活动传递到 Kotlin 活动 Java活动ProfileActivity class Intent selectGameIntent new Intent ProfileActivity this kotlin
  • MediaPlayer.getDuration() 返回错误的持续时间

    媒体播放器的getDuration 方法为我提供了某些音频文件的错误值 我认为所有这些文件的共同特征是它们是使用 Audacity 或其他一些音频编辑工具进行操作的 当尝试将 MediaPlayer 进度绑定到进度栏时 这是一个问题 我继续
  • 如何管理循环器和线程(线程不再消亡!)

    我创建了一个扩展 Thread 的类 以通过非 ui 线程中的 LocationManager 检索用户位置 我将其实现为一个线程 因为它必须根据请求启动并仅在有限的时间内完成其工作 顺便说一句 我必须在线程中添加一个 Looper 对象
  • 使用 jenkins.Creating .apk 文件生成 android 版本

    我正在使用 Jenkins 在 mac 上持续集成 android 应用程序 但是我无法使用 Jenkins 生成 apk 文件 就像我们在 iOS 应用程序中创建 ipa 一样 创建用于在 mac 上分发的 apk 文件的配置是什么 您可
  • 如何强制元数据值类型为字符串?

    我在manifest xml中指定了一个元数据 如下所示
  • [本机]:在Qt for Android中使用Java函数和第3方库[关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 最近我用qt写了一个android应用程序 但我有一个很大的问题 我可以使用 调用一些原生的android API 比如调用特殊的activit
  • 如何指定使用Glide for Android加载图片的重试次数?

    我正在为我的 Android 应用程序使用 glide 库 我想告诉它在放弃并显示错误占位符图像之前重试获取图像 X 次 可能使用指数退避 知道如何做到这一点吗 顺便说一句 我正在使用 Volley 集成 使用您自己的资源解码器 我仅加载本
  • smoothScrollToPosition() 在 Android ICS 中只能滚动到一半?

    在 Gingerbread 中 我使用 smoothScrollToPosition 一次滚动数十个项目没有任何问题 在我的 Nexus S 升级到 Ice Cream Sandwich 后 我注意到无论我在 smoothScrollToP

随机推荐