Tap*_*tro 12 java xml android background image
我一直在尝试用XML创建一个多色背景,但只有3个选项可用的开始,中心,结束和指定角度.我们不能在下面制作这样的背景..
我们能在android中做出这样的背景吗?
Ham*_*mza 25
根据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>
Run Code Online (Sandbox Code Playgroud)
也是这里的一个教程
希望这可以帮助
Har*_*ngh 25
这可以通过使用矢量图形实现
这是此 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>
Run Code Online (Sandbox Code Playgroud)
Pel*_*nes 23
您无法在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);
Run Code Online (Sandbox Code Playgroud)
小智 6
在 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>
Run Code Online (Sandbox Code Playgroud)