在 Android 上绘制形状的顶部边框
我正在创建一个自定义进度条(位于 WebView
下),我想在 WebView
和 ProgressBar 之间绘制一条 1dp 宽的线
。我正在修改现有的可绘制对象,即 progress_horizontal.xml
,并尝试了如下操作:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
(...)
<item>
<shape android:shape="line">
<stroke android:width="1dp" android:color="#FF000000" />
</shape>
</item>
</layer-list>
然而,这条线是垂直居中的,但我希望它绘制在可绘制对象的顶部。我能想到的唯一想法是使用下面这个“hacky”渐变:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
(...)
<item>
<shape>
<gradient
android:startColor="#FF000000"
android:centerColor="#00000000"
android:centerY="0.01"
android:endColor="#00000000"
android:angle="270"
/>
</shape>
</item>
</layer-list>
您有更好的想法如何绘制与层列表定义的可绘制对象顶部对齐的单线形状吗?
I'm creating a custom progress bar (positioned under a WebView
) and what I would like to draw is a 1dp-wide line between the WebView
and the ProgressBar
. I'm modifying existing drawable, namely progress_horizontal.xml
, and tried something like this:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
(...)
<item>
<shape android:shape="line">
<stroke android:width="1dp" android:color="#FF000000" />
</shape>
</item>
</layer-list>
This line however is vertically centered but I want it to be drawn on top of the drawable. The only idea I could come up with is to use this "hacky" gradient below:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
(...)
<item>
<shape>
<gradient
android:startColor="#FF000000"
android:centerColor="#00000000"
android:centerY="0.01"
android:endColor="#00000000"
android:angle="270"
/>
</shape>
</item>
</layer-list>
Do you have better ideas how to draw a single line shape aligned to the top of the drawable defined with layer-list
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我也花了一段时间试图弄清楚这一点。希望有更好的方法来做到这一点,但这是我使用的方法,这也有点黑客,但如果它对某人有帮助,我想我会分享。
图层列表中的第一项应该是您想要作为边框的任何颜色的纯色。接下来是您想要有边框的东西,在您想要边框的任何一侧都有填充。
例如:
这个想法是项目上的填充显示其后面的实体形状,从而提供边框外观。您可以在任意一侧添加内边距以添加边框。我想你可以变得更复杂,并且通过这种方式也可以有不同颜色的边框。
看起来像是一个黑客,但它对我有用。
编辑:我说的是“填充”,但在图层列表中它更多的是偏移。
I spent a while trying to figure this out as well. Hopefully there is a better way to do it but here is the method I used, which is also kind of hackish, but in case it helps someone, I thought I would share.
The first item in your layer list should be a solid of whatever color you want to be your border. Following that would be the thing(s) you want to have the border, with padding on whatever side you want the border to be on.
For example:
The idea is that the padding on the item reveals the solid shape behind it, giving the border appearance. You could add padding to any side to add a border. I imagine you could get more complicated and have different colored borders this way as well.
Seems like a hack but it worked for me.
EDIT: I said "padding" but in layer-lists it's more of an offset.
我正在浏览所有相关主题,但没有人能解决我的问题。但其中一些对于理解图层列表或形状参数的工作原理非常有用。
我的问题是定义一个具有线性渐变的按钮并用不同的颜色绘制顶线和底线。毕竟我破解了这个解决方案。我将文件保存在 res/drawable/blue_btn.xml 下
i was going through all related topics but no one could solve my problem. But some of them were very useful to understand how the layer-list or shape parameters work.
My problem was to define a button with a linear gradient and draw a top and a bottom line in different colors. After all I hacked this solution. I saved the file unter res/drawable/blue_btn.xml
你有没有尝试用这样的东西来抵消它
http://android.amberfog.com/?p=9
Did you try offsetting it by something like this
http://android.amberfog.com/?p=9
我的解决方案是添加 9-patch 作为最后一层项目:
My solution is to add 9-patch as the last layer item :