Java 我们可以在 xml 中为 android 背景制作多色渐变吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42529142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
can we make multi color gradient in xml for android background?
提问by Tapan Kumar Patro
I have been trying to make a multi-color background in XML but there only 3 option available start, center, end and specified angles. Can't we make backgrounds like this below..
我一直在尝试在 XML 中制作多色背景,但只有 3 个选项可用开始、中心、结束和指定的角度。我们不能在下面制作这样的背景..
Can we make like this background in android ??
我们可以在android中制作这样的背景吗??
回答by Hamza
According to developers.androidyou can... and this is the code they used
根据developers.android你可以......这是他们使用的代码
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="45"
android:endColor="#87CEEB"
android:centerColor="#768087"
android:startColor="#000"
android:type="linear" />
</shape>
also here'sa tutorial
也是这里的一个教程
hope this helps
希望这可以帮助
回答by Pete
You can do it with layer-list of radial gradients, then you set different opacity for different items
您可以使用径向渐变的图层列表来完成,然后为不同的项目设置不同的不透明度
回答by Pelanes
You cannot implement +3 gradient color in a xml file. But you can do it into your java/kotlin code with GradientDrawable class. This is the Java version, replace the array of colors with your colors ids.
您不能在 xml 文件中实现 +3 渐变颜色。但是您可以使用 GradientDrawable 类将其添加到您的 java/kotlin 代码中。这是 Java 版本,用您的颜色 ID 替换颜色数组。
GradientDrawable gradientDrawable = new GradientDrawable(
Orientation.TOP_BOTTOM,
new int[]{ContextCompat.getColor(this, R.color.color1),
ContextCompat.getColor(this, R.color.color2),
ContextCompat.getColor(this, R.color.color3),
ContextCompat.getColor(this, R.color.color4)});
findViewById(R.id.background).setBackground(gradientDrawable);
回答by Maryam Azhdari
Create a new xml file in drawable and copy this code:
在 drawable 中创建一个新的 xml 文件并复制以下代码:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:startColor="#9A0C0C"
android:centerColor="#CE9908"
android:endColor="#3091FF"
android:angle="270"/>
</shape>
回答by Harvinder Singh
This can be achieved by using vector graphics i have made this background gradient in adobe illustrator and then import that vector asset as xml in to android studio and it works perfectly with the scalability of vectors
这可以通过使用矢量图形来实现,我在 adobe illustrator 中制作了这个背景渐变,然后将该矢量资产作为 xml 导入到 android studio 中,它与矢量的可扩展性完美配合
and here is the code for this vector/xml drawable
这是此 vector/xml drawable 的代码
<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt"
android:width="987.3dp"
android:height="870.3dp"
android:viewportWidth="987.3"
android:viewportHeight="870.3">
<path android:pathData="M0,870l0,-870l987,0l0,870z">
<aapt:attr name="android:fillColor">
<gradient
android:endX="493.5"
android:endY="870"
android:startX="493.5"
android:startY="2.6645353E-14"
android:type="linear">
<item
android:color="#FF0000FF"
android:offset="0" />
<item
android:color="#FF6AFCFF"
android:offset="0.1974" />
<item
android:color="#FFE900D0"
android:offset="0.3786" />
<item
android:color="#FFFF7D15"
android:offset="0.5906" />
<item
android:color="#FFE6FF55"
android:offset="0.7513" />
<item
android:color="#FFED1E79"
android:offset="1" />
</gradient>
</aapt:attr>
</path>