【问题标题】:Android XML - Apply and use different buttonStylesAndroid XML - 应用和使用不同的按钮样式
【发布时间】:2019-01-23 08:18:28
【问题描述】:

此刻,我觉得有点愚蠢。 这是我的问题: 我实际上想为某些按钮应用不同的样式,与我的默认按钮样式不同。

我的styles.xml

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="CustomTheme" parent="AppTheme">
        <item name="android:actionBarStyle">@style/CustomTheme.ActionBarStyle</item>
        <item name="cardViewStyle">@style/CustomTheme.CardViewStyle</item>
        <item name="buttonStyle">@style/CustomTheme.StandardButton</item>
    </style>

    <...>

    <style name="CustomTheme.StandardButton" parent="Base.Widget.AppCompat.Button.Colored">
        <item name="background">@drawable/ripple_button</item>
        <item name="android:tint">@color/white</item>
        <item name="android:elevation">8dp</item>
    </style>

    <style name="CustomTheme.ConfirmButton" parent="Base.Widget.AppCompat.Button.Colored">
        <item name="background">@drawable/ripple_button_confirm</item>
        <item name="android:tint">@color/white</item>
        <item name="android:elevation">8dp</item>
    </style>
    <style name="CustomTheme.CancelButton" parent="Base.Widget.AppCompat.Button.Colored">
        <item name="background">@drawable/ripple_button_cancel</item>
        <item name="android:tint">@color/white</item>
        <item name="android:elevation">8dp</item>
    </style>

</resources>

事实上,我尝试了很多关于继承 CustomTheme.ConfirmButtonCustomTheme.CancelButton 的事情。

CustomTheme.StandardButton 的变化:形状的颜色。 ripple_button.xml 是橙色的。 然后,我创建了另外两个波纹按钮第一个是红色(取消按钮),第二个是绿色(确认按钮)。

默认情况下,应用&lt;item name="buttonStyle"&gt;@style/CustomTheme.StandardButton&lt;/item&gt;

但是,在我的活动中,即使我为按钮声明了特定主题,默认样式也是唯一应用的样式。 注意:这两个按钮在这些活动中始终可见。

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <Button
        android:id="@+id/btn_confirm_reset"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/lbl_confirm"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/btn_cancel_reset"
        android:layout_marginBottom="8dp"
        style="@style/CustomTheme.ConfirmButton"/>

    <Button
        android:id="@+id/btn_cancel_reset"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/lbl_cancel"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toRightOf="@id/btn_confirm_reset"
        android:layout_marginBottom="8dp"
        style="@style/CustomTheme.CancelButton"/>
</android.support.constraint.ConstraintLayout>

我尝试为每个按钮手动应用“默认”StandardButton 主题(然后从我的styles.xml 中删除&lt;item name="buttonStyle"&gt;@style/CustomTheme.StandardButton&lt;/item&gt; 行,但我仍然得到相同的行为:我所有的按钮总是橙色(并且使用 StandardButton主题)。

所以我想知道我失败的地方/我不明白的地方,或者即使有可能。

如果一个平静的灵魂有一个想法,一些提示,我将不胜感激(我可以用法国精酿啤酒感谢我的英雄)。

祝您有美好的一天,感谢您的阅读。

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    更新您的按钮样式如下:

    style.xml

    <style name="CustomTheme.StandardButton" parent="Base.Widget.AppCompat.Button.Colored">
        <item name="android:backgroundTint">@color/orange</item>
        <item name="android:colorButtonNormal">@color/gray</item>
        <item name="android:colorControlHighlight">@color/blue</item>
        <item name="android:elevation">8dp</item>
    </style>
    
    <style name="CustomTheme.ConfirmButton" parent="Base.Widget.AppCompat.Button.Colored">
        <item name="android:backgroundTint">@color/orange</item>
        <item name="android:colorButtonNormal">@color/gray</item>
        <item name="android:colorControlHighlight">@color/blue</item>
        <item name="android:elevation">8dp</item>
    </style>
    
    <style name="CustomTheme.CancelButton" parent="Base.Widget.AppCompat.Button.Colored">
        <item name="android:backgroundTint">@color/orange</item>
        <item name="android:colorButtonNormal">@color/gray</item>
        <item name="android:colorControlHighlight">@color/blue</item>
        <item name="android:elevation">8dp</item>
    </style>
    

    您错过了在项目背景中附加android:

    要设置波纹效果,您必须使用android:colorControlHighlight,因为android:backgroundTint 将只接受颜色作为输入,而drawable 将不起作用。这里android:colorButtonNormal 将不起作用,因为我们设置了backgroundTint。如果您将删除backgroundTint,它将从android:colorButtonNormal 中获取颜色

    您必须在Button 中设置主题而不是样式。

    your_layout.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <Button
            android:id="@+id/btn_confirm_reset"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/lbl_confirm"
            android:textColor="@color/white"
            android:theme="@style/CustomTheme.ConfirmButton"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@id/btn_cancel_reset"
            app:layout_constraintTop_toTopOf="parent" />
    
        <Button
            android:id="@+id/btn_cancel_reset"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/lbl_cancel"
            android:textColor="@color/white"
            android:theme="@style/CustomTheme.CancelButton"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toRightOf="@id/btn_confirm_reset"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </android.support.constraint.ConstraintLayout>
    

    希望它会起作用!

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-13
    • 2021-07-01
    • 2012-06-16
    • 1970-01-01
    • 2017-10-28
    • 2013-12-23
    • 2013-03-15
    • 1970-01-01
    相关资源
    最近更新 更多