【问题标题】:Android: theme.xml file in values overriding gradient_file.xml in drawableAndroid:theme.xml 文件中的值覆盖 drawable 中的 gradient_file.xml
【发布时间】:2021-01-13 00:33:50
【问题描述】:

我正在尝试使用 android,我正在尝试在 drawable 中设置带有渐变文件的按钮的背景。
渐变文件:

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

    <gradient
        android:startColor="@color/white"
        android:endColor="@color/black"
        android:angle="45"
        android:type="linear" />
</shape>

按钮:

<Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:background="@drawable/gradient_file"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.243" />

现在按钮的颜色不会改变,直到我手动转到 theme.xml 中的值并将原色设置为 null。

theme.xml

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.MyNewUIDesign" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@null</item> <!-- This was set to @color/purple_500 but if i dont make it null my gradient_file is not applied -->
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
    </style>
</resources>

现在唯一的问题是所有按钮都使用该颜色作为默认颜色,如果我将其设置为 null,则其他按钮会失去颜色。如果我不将其设置为 null,则不会应用我的 gradient_file。我只想解决这个问题。

【问题讨论】:

    标签: android material-design android-styles


    【解决方案1】:

    您只需在按钮上添加app:backgroundTint="@null" 即可避免占用colorPrimary 的阴影

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:background="@drawable/gradient_file"
        android:text="Button"
        app:backgroundTint="@null"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.243" />
    

    【讨论】:

    • 感谢它的工作。你能告诉我 backgroundTint 是什么以及为什么从 app:backgroundTint 而不是 android:backgroundTint 使用它吗?
    • 很高兴帮助.. android 命名空间从 android 标准库中获取属性,但 app 命名空间用作 android 库的扩展并称为 support libraries,可以也兼容没有这些库的旧设备..请检查here
    猜你喜欢
    • 2014-07-19
    • 2013-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-30
    • 2011-06-06
    • 2011-06-24
    • 1970-01-01
    相关资源
    最近更新 更多