Android 开放式安卓中风?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2422120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:49:15  来源:igfitidea点击:

Open-sided Android stroke?

androidandroid-drawableandroid-shape

提问by Reed Morse

Is it possible to create an Android shape object with stroke on only certain sides?

是否可以创建仅在某些侧面具有笔划的 Android 形状对象?

Eg I have:

例如我有:

<stroke 
 android:width="3dip" 
 android:color="#000000"
    android:dashWidth="10dip" 
    android:dashGap="6dip" />

Which is similar to this CSS:

这类似于这个CSS:

border: 3px dashed black;

How can I set the stroke on just one side? This is how I would do it in CSS:

如何仅在一侧设置笔划?这就是我在 CSS 中的做法:

border-left: 3px dashed black;

How do you do this in Android XML?

你如何在 Android XML 中做到这一点?

回答by htafoya

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 backgroundbut 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 Maragues answer.

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

EDIT 1

编辑 1

Sometimes, for High Density devices, using low dip values may end in very thin or invisible strokes or distances. This may happen to you also when setting ListView dividers.

有时,对于高密度设备,使用低倾角值可能会导致非常细或不可见的笔划或距离。在设置 ListView 分隔符时,这也可能发生在您身上。

The simplest workaround is to use a distance of 1px instead of 1dp. This will make the line always visible at all densities. The best solution would be to create dimension resources for each density, to get the best size for each device.

最简单的解决方法是使用 1px 的距离而不是 1dp。这将使该线在所有密度下始终可见。最好的解决方案是为每个密度创建维度资源,以获得每个设备的最佳尺寸。

Edit 2

编辑 2

Fun, but I tried to use this 6 years later and I can't get a good result on Lollipop devices.

有趣,但我在 6 年后尝试使用它,但在 Lollipop 设备上无法获得良好的结果。

Probably the current solution is to use 9-patch. Android should have made an easy solution for this problem after all this time.

可能当前的解决方案是使用 9-patch。过了这么久,Android 应该已经为这个问题提供了一个简单的解决方案。

回答by Nirmal Dhara

I know this question was posted long ago, so the answer will not be used by the person who posted this question, but it may still help others.

我知道这个问题是很久以前发布的,所以这个答案不会被发布这个问题的人使用,但它仍然可以帮助其他人。

 <?xml version="1.0" encoding="UTF-8"?>
    <!-- inset is used to remove border from top, it can remove border from any other side-->
    <inset xmlns:android="http://schemas.android.com/apk/res/android"
        android:insetTop="-2dp"
        >
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rectangle">
        <stroke android:width="1dp" android:color="#b7b7b7" />
        <corners android:bottomRightRadius="5dp"  android:bottomLeftRadius="5dp"/>

        <solid android:color="#454444"/>
    </shape>
    </inset>

Use the insettag and give a negative value for the side border you want to remove.

使用inset标签并为要删除的侧边框指定一个负值。

The possible values are:

可能的值为:

android:insetTop="-1dp" android:insetBottom="-1dp" android:insetLeft="-1dp" android:insetRight="-1dp"

android:insetTop="-1dp" android:insetBottom="-1dp" android:insetLeft="-1dp" android:insetRight="-1dp"

回答by Maragues

I solved this by using a list-layer, combining two shapes, one of which with height 1dp placed at the bottom

我通过使用列表层解决了这个问题,结合了两个形状,其中一个高度为 1dp 放在底部

optionscreen_bottomrectangle.xml:

optionscreen_bottomrectangle.xml:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- This is the line -->
<item>
      <shape>
            <solid android:color="#535353" />
      </shape>
</item>
<!-- This is the main color -->
<item android:bottom="1dp">
     <shape>
           <solid android:color="#252525" />
     </shape>
</item>
</layer-list>

Then, in the layout/main.xml file

然后,在 layout/main.xml 文件中

<TextView
    android:id="@+id/bottom_rectangle"
    android:background="@drawable/optionscreen_bottomrectangle"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_below="@id/table_options"
    android:layout_above="@id/exit_bar"/>

Which fills the gap between table_options and exit_bar with a background and just before exit_bar prints a 1dp line. This did the trick for me, I hope it helps someone else.

它用背景填充了 table_options 和 exit_bar 之间的空白,就在 exit_bar 打印 1dp 线之前。这对我有用,我希望它可以帮助其他人。

Answer edited to place layers in the correct order. Thx Alex!

答案已编辑以按正确顺序放置图层。谢谢亚历克斯!

回答by ug_

Another take on the other responses using padding instead. This little snipplet makes a border on top and bottom.

另一种使用填充代替其他响应。这个小 snipplet 在顶部和底部做了一个边框。

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- This is the line -->
    <item>
          <shape>
                <padding android:left="0dp" android:top="1dp" android:right="0dp" android:bottom="1dp"/>
                <solid android:color="#898989" />
          </shape>
    </item>
    <!-- This is the main color -->
    <item>
         <shape>
             <solid android:color="#ffffff" />
         </shape>
    </item>
</layer-list>

回答by Alex Pretzlav

@Maragues's answer is backwards, as layer-list drawables draw from top to bottom (meaning the last item in the list is drawn on top):

@Maragues 的答案是倒退,因为图层列表可绘制对象从上到下绘制(意味着列表中的最后一项绘制在顶部):

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- This is the line -->
<item>
      <shape>
            <solid android:color="#535353" />
      </shape>
</item>
<!-- This is the main color -->
<item android:bottom="1dp">
     <shape>
           <solid android:color="#252525" />
     </shape>
</item>

</layer-list>

This will effectively fill the shape with the line color, then draw the background color on top of it, leaving the last 1dp clear for the line color to show through.

这将有效地用线条颜色填充形状,然后在它上面绘制背景颜色,留下最后 1dp 清晰的线条颜色以显示出来。

回答by sunny

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >


<item>
    <shape android:shape="rectangle" >
        <stroke  android:width="2dp"
                 android:color="#BBBBBB" />
        <solid android:color="@android:color/transparent" />
    </shape>
</item>
<item  android:bottom="2dp" >
    <shape android:shape="rectangle" >
        <stroke  android:width="2dp"
                 android:color="@color/main_background_color" />
        <solid android:color="@android:color/transparent" />
    </shape>
</item>

回答by Ken W.

I've used following code.

我使用了以下代码。

<?xml version="1.0" encoding="UTF-8"?>
<!-- inset is used to remove border from top, it can remove border from any other side-->
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetTop="-2dp" android:insetLeft="-2dp" android:insetRight="-2dp"
    >
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rectangle">
        <stroke android:width="1dp" android:color="@color/colorPrimary" />
        <solid android:color="#0000"/>
        <padding android:left="2dp" android:top="2dp" android:bottom="2dp" android:right="2dp"/>
    </shape>
</inset>