【问题标题】:How to inherit style of AlertDialog buttons如何继承 AlertDialog 按钮的样式
【发布时间】:2016-05-13 20:14:24
【问题描述】:

我想在像 AlertDialog 这样的片段中制作按钮样式,我该怎么做 我应该为我的按钮继承或设置哪种样式

我的样式.xml

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:spinnerItemStyle">@style/Spinner</item>
</style>
<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

谢谢

【问题讨论】:

  • 添加一些图片,说明它们目前的样子以及您希望它们的样子。

标签: android button styles


【解决方案1】:

这是我如何在应用程序中完成此操作的一个简单示例。您需要在 AppTheme 中设置一个 alertDialogTheme。您也可以将样式应用于按钮。这是使用您的代码的示例。

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:spinnerItemStyle">@style/Spinner</item>
    <item name="android:alertDialogTheme">@style/AlertDialogTheme</item>
</style>
<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

<style name="AlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="android:textColor">@android:color/black</item>
        <item name="android:textColorPrimary">@android:color/holo_red_dark</item>
        <item name="android:textColorSecondary">@android:color/holo_orange_light</item>
        <item name="colorAccent">@android:color/holo_purple</item>
        <item name="colorControlHighlight">@android:color/holo_purple</item>
        <item name="buttonBarNegativeButtonStyle">@style/NegativeButton</item>
        <item name="buttonBarPositiveButtonStyle">@style/PositiveButton</item>
</style>

<style name="NegativeButton"
       parent="android:Widget.DeviceDefault.Button.Borderless">
    <item name="android:textColor">@android:color/holo_green_dark</item>
</style>

<style name="PositiveButton"
       parent="android:Widget.DeviceDefault.Button.Borderless">
    <item name="android:textColor">@android:color/green</item>
</style>

【讨论】:

  • 为什么你认为 android:textColor 是 holo_green_dark ? (在 AlertDialog 处)有一些绿色,但我想获得未手动设置的原生颜色
  • 这只是您可以自定义的一个示例,您可以将任何您想要的颜色放入其中,也可以将其删除以使用全局。
  • 不,我想制作像默认 AlertDialog 这样的新按钮,而不是自定义 AlertDialog 按钮,然后对另一个按钮执行相同操作。我的问题是默认使用哪种颜色或样式,以及如何将其设置为另一个元素
  • 您可以创建样式并通过 xml 将其设置为按钮,但显然您必须为每个想要设置样式的按钮都这样做。
猜你喜欢
  • 2011-08-09
  • 1970-01-01
  • 2012-02-24
  • 2014-11-27
  • 1970-01-01
  • 1970-01-01
  • 2016-01-27
  • 2017-08-30
  • 2017-04-03
相关资源
最近更新 更多