Android theme style for button not applying

2020-06-01 18:53发布

I have three simple buttons in my main activity view. What I am trying to do is apply a buttonstyle to all my buttons but I fail to do so.

Here's my xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/minecraft_portrait"
    android:alpha="0.75"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/txtAppName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="@string/app_name_readable"
        android:textSize="32sp"
        android:textColor="@android:color/white" />

    <Button
        android:id="@+id/btnCrafting"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="@drawable/button_selector"
        android:onClick="craftingButtonClicked"
        android:text="@string/crafting" />

    <Button
        android:id="@+id/btnServerCommands"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/btnCrafting"
        android:layout_below="@id/btnCrafting"
        android:layout_marginTop="20dp"
        android:background="@drawable/button_selector"
        android:onClick="commandsButtonClicked"
        android:textColor="@android:color/white"
        android:text="@string/servercommands" />

    <Button
        android:id="@+id/btnVideos"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/btnServerCommands"
        android:layout_below="@id/btnServerCommands"
        android:layout_marginTop="20dp"
        android:background="@drawable/button_selector"
        android:onClick="videosButtonClicked"
        android:text="@string/videos" />

</RelativeLayout>

And this is my style.xml from the res/values folder:

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

    <style name="AppBaseTheme" parent="android:Theme.Light">
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <item name="android:buttonStyle">@style/ButtonTheme</item>
    </style>

    <style name="ButtonTheme" parent="android:Widget.Button">
        <item name="android:textColor">@android:color/white</item>
        <item name="android:background">@android:color/darker_gray</item>
        <item name="android:typeface">monospace</item>
        <item name="android:textSize">32sp</item>
    </style>
</resources>

And my theme definition from the manifest file:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
....

Any ideas why the style isn't applying to my buttons? I have set the textcolor of one button to white just to see any difference.

3条回答
时光不老,我们不散
2楼-- · 2020-06-01 19:36

When using the support library the button style must be applied as:

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:buttonStyle">@style/ButtonTheme</item>
    <item name="buttonStyle">@style/ButtonTheme</item>
</style>

Note the duplication but with no namespace

查看更多
Summer. ? 凉城
3楼-- · 2020-06-01 19:58

The code that you post is working. Please add style on button in xml, to see if that is working for you.

<Button
    style="@style/ButtonTheme"

    android:id="@+id/btnCrafting"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:background="@drawable/button_selector"
    android:onClick="craftingButtonClicked"
    android:text="@string/crafting" />
查看更多
虎瘦雄心在
4楼-- · 2020-06-01 19:58

Try running "clean" on the project when changing styles or the manifest file.

查看更多
登录 后发表回答