开放式Android中风?
是否可以创建一个仅在某些侧面具有描边的 Android 形状对象?
例如,我有:
<stroke
android:width="3dip"
android:color="#000000"
android:dashWidth="10dip"
android:dashGap="6dip" />
这与此 CSS 类似:
border: 3px dashed black;
如何将笔画仅设置在一侧?这就是我在 CSS 中的做法:
border-left: 3px dashed black;
How do you do this in Android XML?
Is it possible to create an Android shape object with stroke on only certain sides?
Eg I have:
<stroke
android:width="3dip"
android:color="#000000"
android:dashWidth="10dip"
android:dashGap="6dip" />
Which is similar to this CSS:
border: 3px dashed black;
How can I set the stroke on just one side? This is how I would do it in CSS:
border-left: 3px dashed black;
How do you do this in Android XML?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我用这个实现了一个很好的解决方案:
如果您需要透明背景但仍然需要开放的描边颜色(在我的情况下,我只需要一条底线),这很有效。如果您需要背景颜色,您可以添加纯色形状,如 Maragues 答案中所示。
编辑 1
有时,对于高密度设备,使用低倾角值可能会导致非常细或不可见的笔划或距离。设置 ListView 分隔线时也可能会发生这种情况。
最简单的解决方法是使用 1px 的距离而不是 1dp。这将使线条在所有密度下始终可见。 最好的解决方案是为每个密度创建维度资源,以获得每个设备的最佳尺寸。
编辑 2
有趣,但 6 年后我尝试使用这个,并且我在 Lollipop 设备上无法获得良好的结果。
目前的解决方案可能是使用 9-patch。 Android 应该已经为这个问题找到了一个简单的解决方案。
I achieved a good solution with this one:
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.
EDIT 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.
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.
Edit 2
Fun, but I tried to use this 6 years later and I can't get a good result on Lollipop devices.
Probably the current solution is to use 9-patch. Android should have made an easy solution for this problem after all this time.
我知道这个问题很久以前就发布了,所以发布这个问题的人不会使用答案,但它仍然可能对其他人有帮助。
使用
inset
标记并为要删除的侧边框指定负值。可能的值为:
android:insetTop="-1dp"
机器人:insetBottom =“-1dp”
机器人:insetLeft =“-1dp”
android:insetRight="-1dp"
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.
Use the
inset
tag and give a negative value for the side border you want to remove.The possible values are:
android:insetTop="-1dp"
android:insetBottom="-1dp"
android:insetLeft="-1dp"
android:insetRight="-1dp"
我通过使用列表层解决了这个问题,组合了两个形状,其中一个高度为 1dp 放置在底部
optionscreen_bottomrectangle.xml 中:
然后,在布局/main.xml 文件中
,该文件用背景填充了 table_options 和 exit_bar 之间的间隙,就在 exit_bar 打印 1dp 行之前。这对我有用,我希望它对其他人有帮助。
答案已编辑,以正确的顺序放置图层。谢谢亚历克斯!
I solved this by using a list-layer, combining two shapes, one of which with height 1dp placed at the bottom
optionscreen_bottomrectangle.xml:
Then, in the layout/main.xml file
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.
Answer edited to place layers in the correct order. Thx Alex!
另一种使用填充代替其他响应的做法。这个小片段在顶部和底部形成了边框。
Another take on the other responses using padding instead. This little snipplet makes a border on top and bottom.
@Maragues 的答案是向后的,因为图层列表可绘制对象从上到下绘制(意味着列表中的最后一项绘制在顶部):
这将有效地用线条颜色填充形状,然后在其顶部绘制背景颜色,将最后 1dp 留空,以便线条颜色显示出来。
@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):
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.
我使用了以下代码。
I've used following code.
简单而高效
要拥有一个透明的视图,并且外部的所有内容都有某种颜色的覆盖,您可以做的是构建封装中心视图的视图,而外部视图不会踩到中心视图。
这样您就可以避免在布局外部使用可绘制对象,并且可以更好地控制视图的距离,而无需在运行时执行计算。
GL
Simple and efficient
To have a transparent view and everything that is outside has an overlay of some color, what you can do is build the views that encapsulate the center view without the outside views stepping on the center view.
This way you avoid having a drawable external to the layout and you have more control over the distances of the views without having to perform calculations at runtime.
GL