首页 > 解决方案 > 可绘制渐变在预览中看起来不错,但在实际应用中被破坏了

问题描述

所以,我想给我的约束布局一个以 colorSecondary 开头并以 colorPrimary 结尾的渐变。这是我的 activity_home.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    android:background="@drawable/gradient_home"
    tools:context=".HomeActivity">

</androidx.constraintlayout.widget.ConstraintLayout>

可绘制/gradient_home.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:type="linear"
        android:angle="180"
        android:startColor="@color/colorSecondary"
        android:endColor="@color/colorPrimary" />
</shape>

这是预览:https ://imgur.com/a/xx5smf4 这是实际应用程序:https ://imgur.com/a/FOwygyB 为什么不匹配?我能做什么?其他文件:attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="colorPrimaryLight" format="color"/>
    <attr name="colorSecondary" format="color"/>
    <attr name="colorSecondaryLight" format="color"/>
    <attr name="colorSecondaryDark" format="color"/>
</resources>

颜色.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#202020</color>
    <color name="colorPrimaryDark">#000000</color>
    <color name="colorPrimaryLight">#474747</color>
    <color name="colorSecondary">#121212</color>
    <color name="colorSecondaryLight">#383838</color>
    <color name="colorSecondaryDark">#000000</color>
    <color name="colorAccent">#ffffff</color>
</resources>

样式.xml

<resources>

    <!-- Base application theme. -->
    <style name="BaseAppTheme" parent="Theme.AppCompat.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="colorPrimaryLight">@color/colorPrimaryLight</item>
        <item name="colorSecondary">@color/colorSecondary</item>
        <item name="colorSecondaryLight">@color/colorSecondaryLight</item>
        <item name="colorSecondaryDark">@color/colorSecondaryDark</item>

    </style>


</resources>

标签: androidgradientlinear-gradients

解决方案


推荐阅读