作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试创建一个带有这样背景的底栏:
我目前正在使用以下代码:
<?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>
但是输出看起来像这样......:
如何得到这个,两个彩色背景?请建议..谢谢
最佳答案
正如其他人所指出的,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/
我是一名优秀的程序员,十分优秀!