【问题标题】:Android LinearLayout Gradient BackgroundAndroid LinearLayout 渐变背景
【发布时间】:2011-08-24 00:35:13
【问题描述】:

我在将渐变背景应用到 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 android-layout android-linearlayout gradient


【解决方案1】:

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

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>

希望这可以帮助遇到同样问题的人。

【讨论】:

【解决方案2】:

也可以有第三种颜色(中心)。还有各种形状。

例如在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>

这为您提供黑色-灰色-黑色(从左到右),这是我最喜欢的深色背景 atm。

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

android:background="@drawable/gradient"

也可以旋转,有:

角度=“0”

给你一条垂直线

与

角度=“90”

给你一条水平线

可能的角度是:

0、90、180、270。

还有几种不同的形状:

android:shape="矩形"

圆形:

android:shape="椭圆"

可能还有更多。

希望对你有帮助,干杯!

【讨论】:

    【解决方案3】:

    在 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 - 要使状态栏透明,您需要在您的 MainActivity Java 文件中的 onCreate() 方法中但在 setContentView() 之后添加它: getWindow().setFlags( WindowManager.LayoutParams. FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
    【解决方案4】:

    尝试删除 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>
    

    【讨论】:

    • 不幸的是,这对我不起作用。我已经用我现在所拥有的内容更新了原始问题。
    • 在布局中添加小部件(例如TextView)是否仍然不起作用?
    • 正确 - 它仍然不适用于布局内的 TextView。同样,如果我应用静态颜色而不是可绘制颜色,它就可以正常工作。我注意到的一件事是我可以(有时)使用选择器让它工作,但根据我的理解,这不应该是必要的。
    【解决方案5】:

    使用 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 扩展名解决了我的问题。

      【讨论】:

        【解决方案7】:

        我会在渐变颜色上检查您的 Alpha 通道。对我来说,当我测试我的代码时,我在颜色上设置了错误的 alpha 通道,它对我不起作用。一旦我设置了 alpha 通道,它就可以工作了!

        【讨论】:

          【解决方案8】:

          我不知道这是否会帮助任何人,但我的问题是我试图将渐变设置为 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"/>
          

          不是 100% 确定为什么这不起作用,但现在我更改了它并将可绘制对象放在 ImageView 父级的“背景”属性中,在我的情况下这是一个相对布局,如下所示:(成功)

          <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">
          

          【讨论】:

            【解决方案9】:

            您可以使用自定义视图来执行此操作。有了这个解决方案,它就完成了你项目中所有颜色的渐变形状:

            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'
            

            【讨论】:

            【解决方案10】:
            <?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"/>
            

            【讨论】:

            • 你把这个放在哪个文件里?你如何调用/使用它?它不应该有名字或id吗?
            猜你喜欢
            • 1970-01-01
            • 2020-03-10
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-12-03
            相关资源
            最近更新 更多