带两种颜色的带状背景? [英] banded background with two colors?

查看:36
本文介绍了带两种颜色的带状背景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用这样的背景创建一个底部栏:

I am trying to create a bottom bar with the background like this :

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

I am currently using the following code :

<?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>

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

But the output looks like this ... :

如何获得这两种颜色的背景?请建议..谢谢

How to get this ,two colored background ? Please suggest.. Thanks

推荐答案

正如其他人所指出的,在这种情况下,九补丁将是理想的(并且不会占用太多内存).处理 XML 并不是最佳选择.您可以尝试以下方法:

As others pointed out, a ninepatch would be ideal in this situation (and won't take much memory). Working around with XML is not optimal. Here is something you can try:

<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>

在这种情况下,您的视图被认为是 40dp 高

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

It's a layer list with two blocks in different colors. The problem with the XML approach is that you can't adjust the height of the blocks to a percentage (=50%). You have to use half of the actual view size in dp. Which means you have to adjust this drawable each time you change your view height/layout. A ninepatch would adjust to this automatically.

这篇关于带两种颜色的带状背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆