Android xml形状drawable - 如何绘制u形式?

时间:2023-02-09 23:55:42

I need to create a xml shape drawable that draws a rectangle without the top-line (a "u-form"). What I am able to do is draw a rectangle, like so:

我需要创建一个xml形状drawable,绘制一个没有顶线的矩形(“u-form”)。我能做的是绘制一个矩形,如下所示:

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

But how - if possible, can I define the same shape without the top (or bottom) line?

但是,如果可能的话,我可以在没有顶部(或底部)线的情况下定义相同的形状吗?

3 个解决方案

#1


10  

You could try using a layer-list. See if something like this would work:

您可以尝试使用图层列表。看看这样的东西是否会起作用:

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

This (should) fill the rectangle with the border color, then overlay it with the default background color, leaving the appropriate amount of the right/left/bottom border color showing.

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

#2


2  

try this code and also refer this link:::

尝试此代码,并参考此链接:::

I achieved a good solution with this one:

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

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

This works well in case you need a transparent color but still an open stroke color (In my case i only needed a bottom line). If you need a background color you can add a solid shape color as in above answer.

如果您需要透明的颜色但仍然是一个开放的笔触颜色(在我的情况下,我只需要一个底线),这很有效。如果您需要背景颜色,可以添加实心形状颜色,如上面的答案。

#3


0  

You might be able to achieve this using two shapes and a LayerList, but I think it is both a better solution and easier to use a NinePatch-drawable.

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

#1


10  

You could try using a layer-list. See if something like this would work:

您可以尝试使用图层列表。看看这样的东西是否会起作用:

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

This (should) fill the rectangle with the border color, then overlay it with the default background color, leaving the appropriate amount of the right/left/bottom border color showing.

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

#2


2  

try this code and also refer this link:::

尝试此代码,并参考此链接:::

I achieved a good solution with this one:

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

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

This works well in case you need a transparent color but still an open stroke color (In my case i only needed a bottom line). If you need a background color you can add a solid shape color as in above answer.

如果您需要透明的颜色但仍然是一个开放的笔触颜色(在我的情况下,我只需要一个底线),这很有效。如果您需要背景颜色,可以添加实心形状颜色,如上面的答案。

#3


0  

You might be able to achieve this using two shapes and a LayerList, but I think it is both a better solution and easier to use a NinePatch-drawable.

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