【问题标题】:AppCompatButton and buttonStyleAppCompatButton 和 buttonStyle
【发布时间】:2017-01-20 22:47:52
【问题描述】:

我正在尝试让一些按钮使用 AppCompat 库的 Android 4.4 设备的 Material Design 样式(特别是 AccentColor 着色)。我在以下方面取得了成功:

        <android.support.v7.widget.AppCompatButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/continue_button"
            android:id="@+id/continue_button"
            android:layout_gravity="center_horizontal|bottom"
            style="@style/CompatButton"
            android:theme="@style/ThemeOverlay.AppCompat.Dark"/>

其中“@style/CompatButton”的父级具有“Widget.AppCompat.Button.Colored”。但是我的一些按钮是相同的,但我没有在元素中声明样式,而是将样式附加为正在使用的主题中的默认“buttonStyle”:

<style name="AppTheme">
    ...
    <item name="android:buttonStyle">@style/CompatButton</item>
    <item name="colorAccent">@color/flow_accent</item>
    ...
</style>

和

        <android.support.v7.widget.AppCompatButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/continue_button"
            android:id="@+id/continue_button"
            android:layout_gravity="center_horizontal|bottom"
            android:theme="@style/ThemeOverlay.AppCompat.Dark"/>

这些按钮以默认的非材质样式显示。这似乎也发生在 ProgressBar 上。任何人都可以看出这有什么问题吗?是否有无需明确定义按钮样式的解决方法?

【问题讨论】:

  • 在使用 AppCompat 小部件时使用 buttonStyle 而不是 android:buttonStyle(等等)。

标签: android android-appcompat android-theme


【解决方案1】:

糟糕,这太愚蠢了。显然 ThemeOverlay 抹杀了之前的主题,包括 buttonStyle 的定义。为了让它工作,我们必须重新添加 buttonStyle:

    <style name="FlowOverlay" parent="ThemeOverlay.AppCompat.Dark">
        <item name="android:buttonStyle">@style/CompatButton</item>
    </style>

        <android.support.v7.widget.AppCompatButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/continue_button"
            android:id="@+id/continue_button"
            android:layout_gravity="center_horizontal|bottom"
            android:theme="@style/FlowOverlay"/>

虽然这首先失去了我们希望从默认按钮样式中获得的很多便利。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-05
    • 1970-01-01
    • 2016-03-05
    • 2022-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-25
    相关资源
    最近更新 更多