Android主题样式不适用于Button

Android Theme style not applying for Button

关于我的申请风格不适用,显示是这样的,

这是我使用的代码,

-----AndroidManifest.xml-----

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

----themes.xml-----

<resources xmlns:tools="http://schemas.android.com/tools">

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/white</item>
        <item name="colorPrimaryVariant">@color/white</item>
        <item name="colorOnPrimary">@color/text_gray</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/white</item>
        <item name="colorSecondaryVariant">@color/white</item>
        <item name="colorOnSecondary">@color/text_gray</item>

        <item name="android:textViewStyle">@style/Regular_TextView</item>
        <item name="buttonStyle">@style/Rounded_Gray_Button</item>
    </style>

    <style name="Rounded_Gray_Button" parent="android:Widget.Button">
        <item name="android:gravity">center</item>
        <item name="android:minWidth">100dp</item>
        <item name="minHeight">40dp</item>
        <item name="font">@font/sf_ui_display_bold</item>
        <item name="android:textSize">16dp</item>
        <item name="background">@drawable/rounded_dark_gray_bg</item>
        <item name="android:textColor">@color/white</item>
    </style>


    <style name="Rounded_Gray_Stroke_Button" parent="android:Widget.Button">
        <item name="android:gravity">center</item>
        <item name="android:minWidth">100dp</item>
        <item name="minHeight">40dp</item>
        <item name="font">@font/sf_ui_display_bold</item>
        <item name="android:textSize">16dp</item>
        <item name="background">@drawable/rounded_dark_gray_stroke</item>
        <item name="android:textColor">@color/dark_gray</item>
    </style>

</resources>

按钮背景

----rounded_dark_gray_bg.xml----

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <stroke
                android:width="1dp"
                android:color="@color/dark_gray" />
            <solid android:color="@color/dark_gray" />
            <corners android:radius="6dp" />
        </shape>
    </item>
</layer-list>

---rounded_dark_gray_stroke.xml----

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <stroke
                android:width="1dp"
                android:color="@color/dark_gray" />
            <solid android:color="@color/white" />
            <corners android:radius="6dp" />
        </shape>
    </item>
</layer-list>

我正在下面的 xml 文件中应用上面的按钮样式,

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

       <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:orientation="horizontal"
            android:gravity="center"
            android:padding="10dp">

            <Button
                android:id="@+id/btnSignin"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:theme="@style/Rounded_Gray_Button"
                android:text="@string/Sign_in"/>

            <Button
                android:id="@+id/btnRegister"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:theme="@style/Rounded_Gray_Stroke_Button"
                android:text="@string/Register"/>

        </LinearLayout>

</RelativeLayout>

要应用背景,我们需要在 themes.xml

上将 <item name="background">@drawable/rounded_dark_gray_bg</item> 替换为 <item name="android:background">@drawable/rounded_dark_gray_bg</item>

同样要申请我们需要使用 like,

style="@style/Rounded_Gray_Button"

style="@style/Rounded_Gray_Stroke_Button"

您不应在单个组件上设置主题。您应该将您定义的主题属性设置为:buttonStyle,作为组件的样式:

style="?buttonStyle"

...或者你可以直接引用样式,但是不能随主题变化(除非样式中的属性引用主题属性)

style="@style/Rounded_Gray_Button"