首页 > 解决方案 > 将按钮样式添加到 AppTheme 不起作用

问题描述

我尝试向我的 AppTheme 添加自定义按钮样式,但它不起作用。作为参考,我在我的 AppTheme 中添加了 Spinner 样式,它运行良好。所以,我只需要找出我的按钮样式哪里出了问题。

样式.xml:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:spinnerStyle">@style/mySpinnerStyle</item>
    <item name="android:buttonStyle">@style/myButtonStyle</item>
    <item name="buttonStyle">@style/myButtonStyle</item>
</style>

<style name="mySpinnerStyle" parent="@style/Widget.AppCompat.Spinner">
    <item name="overlapAnchor">true</item>
    <item name="android:background">@drawable/spinner_background</item>
    <item name="android:padding">5sp</item>
</style>

<style name="myButtonStyle" parent="@style/Widget.AppCompat.Button">
    <item name="android:background">@drawable/button_states</item>
    <item name="android:focusable">true</item>
    <item name="android:clickable">true</item>
</style>

如果需要,我可以添加我的 button_states.xml 文件,但是如果我将样式直接插入到布局中的按钮属性中,则该样式有效,如下所示:

<Button
    android:id="@+id/button_1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="4"
    style="@style/myButtonStyle"
    android:textSize="@dimen/text_size"
    android:text="OK" />

父布局中的声明:

android:theme="@style/AppTheme"

那么,为什么它直接在按钮属性中起作用,而不是通过我的 AppTheme 呢?

标签: androidbuttonstylesthemes

解决方案


1-通过右键单击您的android studio中的res / drawable文件创建新的DRAWABLE RESOUCE FILE

2-在其中编写这样的代码:-

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <corners
android:radius="100dp"
        />
    <solid
        android:color="#FFFFFF"

        />
    <size
        android:width="100dp"
        android:height="100dp"
        />

</shape>

3-打开包含此按钮的 XML 文件并将按钮的背景更改为您之前创建的可绘制对象


推荐阅读