gpt4 book ai didi

android - 有两种颜色的带状背景?

转载 作者:可可西里 更新时间:2023-11-01 19:12:05 25 4
gpt4 key购买 nike

我正在尝试创建一个带有这样背景的底栏:

enter image description here

我目前正在使用以下代码:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">


<item>
<shape>
<gradient android:endColor="#000000" android:startColor="#696969"
android:centerColor="#696969" android:angle="270" />
<stroke android:color="#696969" />
<padding android:left="2dp" android:top="3dp" android:right="2dp"
android:bottom="3dp" />
</shape>
</item>
</selector>

但是输出看起来像这样......:

enter image description here

如何得到这个,两个彩色背景?请建议..谢谢

最佳答案

正如其他人所指出的,ninepatch 在这种情况下是理想的(并且不会占用太多内存)。使用 XML 并不是最佳选择。您可以尝试以下操作:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:bottom="20dp">
<shape android:shape="rectangle" >
<size android:height="20dp" />
<solid android:color="#ff0000" />
</shape>
</item>

<item android:top="20dp">
<shape android:shape="rectangle" >
<size android:height="20dp" />
<solid android:color="#0000ff" />
</shape>
</item>
</layer-list>

在这种情况下,您的 View 被视为 40dp 高

这是一个图层列表,包含两个不同颜色的 block 。 XML 方法的问题是您无法将 block 的高度调整为百分比 (=50%)。您必须使用 dp 中实际 View 大小的一半。这意味着每次更改 View 高度/布局时都必须调整此可绘制对象。 ninepatch 会自动对此进行调整。

关于android - 有两种颜色的带状背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8727238/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com