安卓LinearLayout渐变背景

293

我尝试将渐变背景应用于LinearLayout,但遇到了问题。

根据我所了解的,这应该是相对简单的,但似乎并不起作用。为了参考,我正在2.1-update1上进行开发。

header_bg.xml:

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

main_header.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="50dip"
    android:orientation="horizontal"
    android:background="@drawable/header_bg">
</LinearLayout>

如果我将@drawable/header_bg更改为颜色 - 例如#FF0000,它可以完美地工作。我是否遗漏了一些显而易见的东西?


android:backgroundTint android:backgroundTintMode https://dev59.com/dWEh5IYBdhLWcg3wbjHc#43341289 - Pramod Garg
10个回答

456

好的,我已经使用选择器解决了这个问题。请参见下面的代码:

main_header.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="50dip"
    android:orientation="horizontal"
    android:background="@drawable/main_header_selector">
</LinearLayout>

main_header_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape>
        <gradient
            android:angle="90"
            android:startColor="#FFFF0000"
            android:endColor="#FF00FF00"
            android:type="linear" />
    </shape>
</item>
</selector>

希望这能帮到有同样问题的人。


5
好的。请参考其他类型的渐变:http://developer.android.com/reference/android/graphics/drawable/GradientDrawable.html#attr_android:type - Bamaco
谢谢,运作得非常完美! - Zachary Smouse

111

还可以拥有第三种颜色(中心颜色),以及不同种类的形状。

例如在drawable/gradient.xml中:

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="#000000"
        android:centerColor="#5b5b5b"
        android:endColor="#000000"
        android:angle="0" />
</shape>

这会给你一个从左到右的黑-灰-黑(我的最喜欢的深色背景)。

记得在你的布局XML中添加gradient.xml作为背景:

android:background="@drawable/gradient"

还可以通过以下方式进行旋转:

angle="0"

将生成一条垂直线。

而使用

angle="90"

会生成一条水平线。

可能的角度为:

0,90,180,270。

此外还有几种不同类型的形状:

android:shape="rectangle"

圆形:

android:shape="oval"

还可能有几个更多的形状。


49

在XML可绘制文件中:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <gradient android:angle="90"
                android:endColor="#9b0493"
                android:startColor="#38068f"
                android:type="linear" />
        </shape>
    </item>
</selector>
在你的布局文件中:android:background="@drawable/gradient_background"
  <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/gradient_background"
        android:orientation="vertical"
        android:padding="20dp">
        .....

</LinearLayout>

在此输入图片描述


你好,你是如何实现透明状态栏的?如果我在styles.xml中将其设置为透明,它会变成黑色.. - kironet
@kironet - 要使状态栏透明,您需要在MainActivity Java文件中的onCreate()方法中添加以下内容,但在setContentView()之后: getWindow().setFlags( WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS ); - Liraz Shaka Amir

21

尝试移除android:gradientRadius="90"。这是一个对我有效的实例:

<shape 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
>
    <gradient
        android:startColor="@color/purple"
        android:endColor="@color/pink"
        android:angle="270" />
</shape>

不幸的是,这对我没有用。我已经更新了原始问题,附上了我现在拥有的内容。 - Genesis
当您在布局中添加小部件(例如TextView)时,它仍然无法正常工作吗? - Vincent Mimoun-Prat
正确 - 在布局中放置TextView仍然无法正常工作。同样,如果我应用静态颜色而不是可绘制对象,则可以正常工作。我注意到的一件事是,有时我可以使用选择器使其正常工作,但根据我的理解,这不应该是必要的。 - Genesis

16

使用 Kotlin 只需 2 行即可完成此操作

更改数组中的颜色值

                  val gradientDrawable = GradientDrawable(
                        GradientDrawable.Orientation.TOP_BOTTOM,
                        intArrayOf(Color.parseColor("#008000"),
                                   Color.parseColor("#ADFF2F"))
                    );
                    gradientDrawable.cornerRadius = 0f;

                   //Set Gradient
                   linearLayout.setBackground(gradientDrawable);

结果

这里输入图片描述


6

我的问题是新创建的XML文件的文件名没有添加.xml扩展名。 添加.xml扩展名解决了我的问题。


0

我会检查一下你的渐变颜色的 alpha 通道。在我测试代码时,我的颜色的 alpha 通道设置错误,导致代码无法正常工作。当我将 alpha 通道设置正确后,一切都正常了!


0

我不知道这是否能帮助到任何人,但是我的问题在于我试图将渐变设置为ImageView的“src”属性,就像这样:

<ImageView 
    android:id="@+id/imgToast"
    android:layout_width="wrap_content"
    android:layout_height="60dp"
    android:src="@drawable/toast_bg"
    android:adjustViewBounds="true"
    android:scaleType="fitXY"/>

不太确定为什么之前那个方法没行,但我现在把可绘制对象(drawable)放到了ImageView的父级RelativeLayout中的“background”属性中,就像这样:(这个方法成功了)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:id="@+id/custom_toast_layout_id"
    android:layout_height="match_parent"
    android:background="@drawable/toast_bg">

0
你可以使用自定义视图来实现这一点。通过这种解决方案,你的项目中所有颜色的渐变形状都可以完成。
class GradientView(context: Context, attrs: AttributeSet) : View(context, attrs) {

    // Properties
    private val paint: Paint = Paint()
    private val rect = Rect()

    //region Attributes
    var start: Int = Color.WHITE
    var end: Int = Color.WHITE
    //endregion

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        // Update Size
        val usableWidth = width - (paddingLeft + paddingRight)
        val usableHeight = height - (paddingTop + paddingBottom)
        rect.right = usableWidth
        rect.bottom = usableHeight
        // Update Color
        paint.shader = LinearGradient(0f, 0f, width.toFloat(), 0f,
                start, end, Shader.TileMode.CLAMP)
        // ReDraw
        invalidate()
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        canvas.drawRect(rect, paint)
    }

}

我还创建了一个开源项目GradientView,使用了这个自定义视图:

https://github.com/lopspower/GradientView

implementation 'com.mikhaellopez:gradientview:1.1.0'

看起来很有趣。这个适用于较新的Android版本吗?因为我在使用其他答案时遇到了问题,因为它们现在似乎已经过时了:https://issuetracker.google.com/issues/37114374 - trees_are_great

-1
<?xml version="1.0" encoding="utf-8"?>

<gradient
    android:angle="90"
    android:startColor="@color/colorPrimary"
    android:endColor="@color/colorPrimary"
    android:centerColor="@color/white"
    android:type="linear"/>

<corners android:bottomRightRadius="10dp"
    android:bottomLeftRadius="10dp"
    android:topRightRadius="10dp"
    android:topLeftRadius="10dp"/>

enter image description here


你应该把它放在哪个文件里?如何调用或使用它?它不应该有一个名称或ID吗? - Houman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接