【问题标题】:Android: Solid color on button not workingAndroid:按钮上的纯色不起作用
【发布时间】:2021-02-20 12:58:42
【问题描述】:

API 22. 我想为我的按钮设置一个边框半径,所以我这样做了:How to make the corners of a button round? (我尝试了正确的答案)。 How to change the color of a button? 这里的任何方法都不起作用。 我曾经使用 android:backgroundTint 设置按钮的背景颜色。那是唯一有效的方法。不幸的是,当我通过 android:background="@drawable/roundbutton" 将文件链接到我的按钮时,我通过 android:backgroundTint 选择的颜色不起作用。以及我在可绘制/圆形按钮中定义的纯色。奇怪的是,边界半径有效: 我在 activity_main.xml 中的代码:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/signinotherbutton"
    android:background="@drawable/roundbutton"
    android:backgroundTint="@color/grey"
    android:textColor="@color/white"
    android:textAllCaps="false"/>

我的 drawable/roundbutton.xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" >
        <shape android:shape="rectangle"  >
            <corners android:radius="3dip" />
            <solid android:color="@color/grey"/>
        </shape>
    </item>
    <item android:state_focused="true">
        <shape android:shape="rectangle"  >
            <corners android:radius="3dip" />
            <solid android:color="@color/grey"/>
        </shape>
    </item>
    <item >
        <shape android:shape="rectangle"  >
            <corners android:radius="3dip" />
            <solid android:color="@color/grey"/>
        </shape>
    </item>
</selector>

结果:具有我定义的半径的按钮,但具有默认颜色(约紫色) 不,@color/grey 绝对是灰色,而不是紫色。 谢谢大家的回答

【问题讨论】:

  • 您在应用中使用的是哪个主题?

标签: android button colors radius


【解决方案1】:

这是我的绿色圆形按钮的代码,您可以根据自己的方便进行修改。 希望这会奏效。

button_rounded.xml

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

【讨论】:

  • 代码和我的有点相似。我会尝试做一个新项目并测试它是否在那里工作。您使用的是哪个 API?
【解决方案2】:

我能够通过在themes.xml 中创建一个新样式并将其设置为我的按钮的主题来解决我的问题。这是我在themes.xml中的代码:

<style name="Theme.ButtonWhite" parent="Base.Widget.AppCompat.Button.Colored">
    <item name="colorPrimary">@color/white</item>
</style>

activity_main.xml 中按钮的代码:

android:theme="@style/Theme.ButtonWhite"

我真的问自己为什么没有其他人似乎有这个问题,因为我在创建一个全新的项目时遇到了完全相同的问题

【讨论】:

    猜你喜欢
    • 2015-11-03
    • 1970-01-01
    • 1970-01-01
    • 2018-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-17
    相关资源
    最近更新 更多