开放式Android中风? [英] Open-sided Android stroke?

查看:33
本文介绍了开放式Android中风?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

例如我有:

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

与此 CSS 相似的内容:

Which is similar to this CSS:

border: 3px dashed black;

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

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

border-left: 3px dashed black;

您如何在 Android XML 中执行此操作?

How do you do this in Android XML?

推荐答案

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

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>

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

This works well in case you need a transparent background 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 Maragues answer.

编辑 1

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

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.

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

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.

编辑 2

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

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

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

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

这篇关于开放式Android中风?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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