Android LinearLayout 渐变背景
我在将渐变背景应用于 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,它工作得很好。我在这里遗漏了一些明显的东西吗?
I am having trouble applying a gradient background to a LinearLayout.
This should be relatively simple from what I have read but it just doesn't seem to work. For reference sakes I am developing on 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>
If I change @drawable/header_bg to a color - e.g. #FF0000 it works perfectly fine. Am I missing something obvious here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
好吧,我已经设法使用选择器解决了这个问题。请参阅下面的代码:
main_header.xml:
main_header_selector.xml:
希望这可以帮助遇到相同问题的人。
Ok I have managed to solve this using a selector. See code below:
main_header.xml:
main_header_selector.xml:
Hopefully this helps someone who has the same problem.
也可以有第三种颜色(中心)。以及各种不同的形状。
例如在drawable/gradient.xml中:
这给你黑色-灰色-黑色(从左到右),这是我最喜欢的深色背景atm。
请记住在布局 xml 中添加gradient.xml 作为背景:
也可以使用以下方法进行旋转:
给你一条垂直
线
给你一条水平线
可能的角度有:
还有几种不同的形状:
圆形:
可能还有更多。
It is also possible to have the third color (center). And different kinds of shapes.
For example in drawable/gradient.xml:
This gives you black - gray - black (left to right) which is my favorite dark background atm.
Remember to add gradient.xml as background in your layout xml:
It is also possible to rotate, with:
gives you a vertical line
and with
gives you a horizontal line
Possible angles are:
Also there are few different kind of shapes:
Rounded shape:
and problably a few more.
在 XML Drawable 文件中:
在布局文件中: android:background="@drawable/gradient_background"
In XML Drawable File:
In your layout file: android:background="@drawable/gradient_background"
尝试删除 android:gradientRadius="90"。这是对我有用的一个:
Try removing android:gradientRadius="90". Here is one that works for me:
使用 Kotlin,您只需 2 行即可完成此操作
更改数组中的颜色值
Result
< img src="https://i.sstatic.net/X7LOx.png" alt="在此处输入图像描述">
With Kotlin you can do that in just 2 lines
Change color values in the array
Result
我的问题是 .xml 扩展名未添加到新创建的 XML 文件的文件名中。添加 .xml 扩展名解决了我的问题。
My problem was the .xml extension was not added to the filename of the newly created XML file. Adding the .xml extension fixed my problem.
我会检查你的渐变颜色的 Alpha 通道。对我来说,当我测试我的代码时,我在颜色上设置了错误的 Alpha 通道,并且它对我不起作用。一旦我设置了 Alpha 通道,一切就正常了!
I would check your alpha channel on your gradient colors. For me, when I was testing my code out I had the alpha channel set wrong on the colors and it did not work for me. Once I got the alpha channel set it all worked!
我不知道这是否会对任何人有帮助,但我的问题是我试图将渐变设置为 ImageView 的“src”属性,如下所示:
不能 100% 确定为什么这不起作用,但现在我更改了它并将可绘制对象放在 ImageView 父级的“背景”属性中,在我的例子中,这是一个relativelayout,如下所示:(这成功了)
I don't know if this will help anybody, but my problem was I was trying to set the gradient to the "src" property of an ImageView like so:
Not 100% sure why that didn't work, but now I changed it and put the drawable in the "background" property of the ImageView's parent, which is a RelativeLayout in my case, like so: (this worked successfully)
您可以使用自定义视图来做到这一点。通过这个解决方案,就完成了项目中所有颜色的渐变形状:
我还创建了一个开源项目 GradientView 具有此自定义视图:
https://github.com/lopspower/GradientView< /a>
You can used a custom view to do that. With this solution, it's finished the gradient shapes of all colors in your projects:
I also create an open source project GradientView with this custom view:
https://github.com/lopspower/GradientView