2011-05-12 136 views
182

我无法将渐变背景应用于LinearLayout。Android 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> 

如果我改变@抽拉/ header_bg到彩色 - 例如#FF0000它工作得很好。我在这里错过了很明显的东西吗

+0

机器人:机器人backgroundTint:backgroundTintMode http://stackoverflow.com/a/43341289/3209132 – 2017-04-28 07:44:27

回答

329

好吧我已经设法解决这个使用选择器。请参见下面的代码:

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

太好了。仅供参考,请参阅其他渐变类型:http://developer.android.com/reference/android/graphics/drawable/GradientDrawable.html#attr_android:type – Bamaco 2016-04-07 21:36:46

15

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

不幸的是,这不适合我。我已经用我现在的东西更新了原始问题。 – Genesis 2011-05-12 11:07:01

+0

在布局中添加小部件时是否仍然无法工作(例如,TextView? – 2011-05-12 11:32:17

+0

正确 - 它仍然不能在布局中使用TextView。如果我应用静态颜色而不是绘制,它只能工作好的。我注意到的一件事是我可以(有时)使用选择器来工作,但根据我的理解,这应该不是必需的。 – Genesis 2011-05-12 13:54:01

0

我会检查你的alpha通道你的渐变颜色。对我来说,当我测试我的代码时,我的alpha通道在颜色上设置错误,并且对我没有任何作用。一旦我得到了alpha通道设置它一切工作!

5

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

28

也有可能有第三种颜色(中心)。和不同种类的形状。

例如,在绘制/ 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作为在布局XML背景:

android:background="@drawable/gradient" 

也可以旋转,用:

角= “0”

为您提供了一个垂直线

并与

angle =“ 90"

给你一个水平线

可能的角度是:

0,90,180,270

而且很少有不同种类的形状:

android:shape =“rectangle”

圆角形状:

机器人:形状= “椭圆形”

和problably几个。

希望它有帮助,欢呼!

1

我不知道这是否会帮助任何人,但我的问题是,我试图梯度设置,像这样的的ImageView的“源”属性:

<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的在我的情况下,像这样的“背景”属性:(这种成功合作)

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

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

在您的布局文件:机器人:背景= “@绘制/ 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> 

enter image description here