【问题标题】:Cannot overlap gradient with a solid color in Android Studio无法在 Android Studio 中将渐变与纯色重叠
【发布时间】:2020-03-08 04:19:51
【问题描述】:

我是在 Android Studio 中创建应用程序的初学者,我正在尝试重新创建在 Figma 中制作的背景。

Figma 中的背景

属性

Rotation: 179.7°
Fill: Linear Gradient
#23152C @ 0.0
#828F8893 @ 0.49
#00FFFFFF @ 1.0
Fill: Solid
#120A16
Effect: Drop Shadow
Radius: 4dp
Offset: 0dp, 4dp
#40000000

在Android Studio中多次尝试重新创建背景后,我发现问题是颜色不重叠,透明的白色#00FFFFFF应该被纯色#120A16替换。

drawables 文件夹中的background.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/solid_grad"/>
    <gradient
        android:startColor="@color/gradientStart"
        android:centerColor="@color/gradientCenter"
        android:endColor="@color/solid_grad"
        android:type="linear"
        android:angle="90" >

    </gradient>

</shape>

values 文件夹中的colors.xml

<resources>
    <color name="gradientStart">#23152C</color>
    <color name="gradientCenter">#828F8893</color>
    <color name="gradientEnd">#00FFFFFF</color>
    <color name="solid_grad">#120A16</color>
</resources>

结果

有人知道如何将渐变与纯色重叠吗?

PS:由于屏幕尺寸的多样性和性能原因,我不想从 Figma 中导出背景并将其导入 Android。

【问题讨论】:

  • 我能够在 angrytools.com/gradient 中重新创建 Figma 渐变
  • 你能添加你的drawable的完整代码吗?
  • 大家好,感谢您检查我的问题,@Skizo-ozᴉʞS,我已经添加了 colors.xml 文件,可绘制代码已经在上面了。
  • @Zun,你能提供它的Android代码吗?不幸的是,我不熟悉该工具。

标签: android android-studio figma-api


【解决方案1】:

这是我的解决方案:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".MainActivity"
    android:background="@drawable/gradient"
    >

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/gradient2">
</RelativeLayout>
</RelativeLayout>

gradient.xml 来自 drawables 文件夹

<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:endColor="#120A16"
        android:startColor="#120A16"
        android:angle="90" />

</shape>

gradient2.xml 来自 drawables 文件夹

<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:endColor="#00FFFFFF"
        android:centerColor="#828F8893"
        android:startColor="#23152C"
        android:angle="90"/>
</shape>

我知道它可以用不同的方式做得更好,但这类似于我在 css 中使用 2 个 DIV 的方式 请,如果您有更好的方法,请告诉我。我的项目也需要这个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-22
    • 2013-07-29
    • 1970-01-01
    • 1970-01-01
    • 2019-07-02
    • 1970-01-01
    • 2016-12-02
    • 2013-03-22
    相关资源
    最近更新 更多