android - 将主题应用于 Android 中的动态单选按钮

标签 android styles radio-button

我的应用程序中有一个主题,它定义了默认的文本外观和按钮样式。

<style name="AppTheme" parent="@android:style/Theme.Light.NoTitleBar.Fullscreen">
    <item name="android:textAppearance">@style/TextAppearance.Medium</item>
    <item name="android:textAppearanceLarge">@style/TextAppearance.Large</item>
    <item name="android:textAppearanceMedium">@style/TextAppearance.Small</item>
    <item name="android:textAppearanceSmall">@style/TextAppearance.Medium</item>
    <item name="android:textColorPrimary">#000000</item>
    <item name="android:textColorSecondary">#000000</item>
    <item name="android:textColorTertiary">#000000</item>
    <item name="android:buttonStyle">@style/Button</item>
</style>

<style name="TextAppearance" parent="@android:style/TextAppearance">
    <item name="android:textColor">?android:attr/textColorPrimary</item>
    <item name="android:textColorHint">?android:attr/textColorHint</item>
</style>

<style name="TextAppearance.Large">
    <item name="android:textColor">?android:attr/textColorPrimary</item>
    <item name="android:textSize">32dp</item>
    <item name="android:textStyle">bold</item>
</style>

<style name="TextAppearance.Medium">
    <item name="android:textColor">?android:attr/textColorSecondary</item>
    <item name="android:textSize">22dp</item>
</style>

<style name="TextAppearance.Small">
    <item name="android:textColor">?android:attr/textColorTertiary</item>
    <item name="android:textSize">18dp</item>
</style>

<style name="Button" parent="@android:style/Widget.Button">
    <item name="android:textColor">?android:attr/textColorSecondary</item>
    <item name="android:textColorHint">?android:attr/textColorHint</item>
    <item name="android:textStyle">normal</item>
    <item name="android:textAppearance">?android:attr/textAppearanceSmall</item>
</style>

当我创建一个 RadioButton 时使用

RadioButton radioButton = new RadioButton(this, null, R.style.AppTheme);

RadioButton 已创建,文本的样式已正确设置,但选中的标记消失了。为什么?

我在 Buttongroup 中有 Radiobutton,我在其中动态添加按钮。当我通过布局添加按钮时,标记会正确显示。但动态添加的按钮不会。

最佳答案

解决方案是使用 单选按钮(这个) 构造函数。

所有使用 defStyle-Constructor 设置的样式都被忽略并且标记被删除。我不确定这是错误还是没有记录。

我实际上不需要它,因为我的 styles.xml 中的其他样式现在自动应用于按钮,但如果有人感兴趣:

添加

<item name="android:radioButtonStyle">@style/MyRadioButton</item>

到我的“AppTheme”-风格和

<style name="MyRadioButton" parent="@android:style/Widget.CompoundButton.RadioButton">
    <item name="android:textSize">5dp</item>
</style>

到 style.xml 使我所有的单选按钮显示标记和文本样式(没有在构造函数中设置它)。

关于android - 将主题应用于 Android 中的动态单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12402593/

相关文章:

android - 使用 Android 连接到本地服务器

c# - 将按钮的文本设置为在 WPF 中有一些粗体字符

javascript - 改进单选按钮的使用以启用/禁用表单字段

jquery - 在 jquery 的数组中检查 radio

android - 怎么知道短信号码和电话号码是一样的?

java - android 发送消息存储失败

javascript - WebView 导致应用程序崩溃

python - 如何通过标签更改Treeview中项目的背景/前景?

Qt QSS 教程

android - 在 Android Studio 中设置默认单选按钮