【问题标题】:How to make a fade black background in Android Studio如何在 Android Studio 中制作渐变黑色背景
【发布时间】:2021-07-31 15:59:52
【问题描述】:

我想用淡黑色覆盖我的图像,就像这个一样,带有文本、淡化背景,然后是我的图像。我只想知道如何实现这个淡入淡出。谢谢!

【问题讨论】:

  • 你试过了吗?
  • @Zain 是的,我在询问后 2 分钟解决了这个问题:D

标签: java android android-studio android-layout android-recyclerview


【解决方案1】:

只需创建一个可绘制文件,我将其命名为gradient_background(您可以设置任何名称)

<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <gradient 
       android:angle="90" 
       android:startColor="#000000"
       android:centerColor="#00000000" 
       android:endColor="#00000000"/>

</shape>

然后在你的target xml中,创建一个像这样的View,看看里面,我们将background设置为你的可绘制文件。在这种情况下是 gradient_background

<View
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:background="@drawable/gradient_background"
    app:layout_constraintBottom_toBottomOf="post_image"
    app:layout_constraintEnd_toEndOf="@+id/post_image"
    app:layout_constraintStart_toStartOf="post_image"
    app:layout_constraintTop_toTopOf="@+id/post_image" />

所以它应该是这样的:

:)

【讨论】:

    【解决方案2】:

    首先,制作bottom_transparent_gradient.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item>
        <shape>
            <gradient android:angle="270"
                android:endColor="#64000000"
                android:startColor="@android:color/transparent" />
        </shape>
      </item>
    </selector>
    

    然后,将该可绘制对象添加为您要在其中显示淡入淡出背景效果的视图背景。

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

    就是这样!你可以看到你想要的结果。 :)

    【讨论】:

      猜你喜欢
      • 2012-12-05
      • 2018-10-24
      • 2020-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-27
      • 1970-01-01
      相关资源
      最近更新 更多