【问题标题】:Android xml shape drawable - how to draw a u-form?Android xml shape drawable - 如何绘制 u-form?
【发布时间】:2012-04-17 01:41:29
【问题描述】:

我需要创建一个可绘制的 xml 形状,它可以绘制一个没有顶线的矩形(“u 形”)。我能做的是画一个矩形,像这样:

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

<solid android:color="@color/detailrow_bg_normal" />

<corners
    android:bottomLeftRadius="0dp"
    android:bottomRightRadius="0dp"
    android:radius="1dp"
    android:topLeftRadius="10dp"
    android:topRightRadius="10dp" />

<padding
    android:bottom="2dip"
    android:left="1.5dip"
    android:right="1.5dip"
    android:top="8dip" />

<stroke
    android:width="1dip"
    android:color="@color/detailtable_border" />

但是,如果可能的话,我如何定义没有顶(或底)线的相同形状?

【问题讨论】:

    标签: android xml shape


    【解决方案1】:

    您可以尝试使用图层列表。看看这样的事情是否可行:

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
        <item>
          <shape android:shape="rectangle">
                <solid android:color="@color/detailtable_border" />
            </shape>
       </item>
    
       <item android:left="1.5dp" android:right="1.5dp" android:bottom="2dp">
          <shape android:shape="rectangle">
            <solid android:color="@color/detailrow_bg_normal" />
          </shape>
       </item>
    </layer-list>
    

    这(应该)用边框颜色填充矩形,然后用默认背景颜色覆盖它,留下适当数量的右/左/下边框颜色显示。

    【讨论】:

    • 优秀:那是盲目的:)。很高兴知道它有帮助。
    【解决方案2】:

    试试这个代码并参考这个link:::

    我用这个实现了一个很好的解决方案:

    <?xml version="1.0" encoding="utf-8"?>
    
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- This is the line -->
        <item android:top="-1dp" android:right="-1dp" android:left="-1dp">
          <shape>
                <solid android:color="@android:color/transparent" />
                <stroke android:width="1dp" android:color="#ffffff" />
          </shape>
        </item>
    
    </layer-list>
    

    如果您需要透明颜色但仍然是开放的笔触颜色(在我的情况下,我只需要底线),这很有效。如果您需要背景颜色,可以按照上述答案添加纯色。

    【讨论】:

      【解决方案3】:

      您也许可以使用两个形状和一个LayerList 来实现这一点,但我认为使用NinePatch-drawable 既是一个更好的解决方案,也更容易。

      【讨论】:

      • 考虑过,但它需要对已经实现的解决方案进行相当大的更改,我真的更喜欢 xml 形状的方式)。
      猜你喜欢
      • 2011-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多