在 Android 上的形状上绘制顶部边框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2224644/
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
Drawing top border on a shape on Android
提问by Marek Stój
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:
我正在创建一个自定义进度条(位于 a 下WebView
),我想绘制的是WebView
和ProgressBar
. 我正在修改现有的 drawable,即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>
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:
然而,这条线垂直居中,但我希望它被绘制在可绘制对象的顶部。我能想出的唯一想法是使用下面这个“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>
Do you have better ideas how to draw a single line shape aligned to the top of the drawable defined with layer-list
?
您是否有更好的想法如何绘制与定义的可绘制对象顶部对齐的单线形状layer-list
?
回答by cottonBallPaws
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.
我也花了一段时间试图弄清楚这一点。希望有更好的方法来做到这一点,但这是我使用的方法,这也是一种hackish,但如果它对某人有帮助,我想我会分享。
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:
例如:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="YOUR BORDER COLOR" />
</shape>
</item>
<item android:top="YOUR TOP BORDER THICKNESS">
THE THING YOU WANT A BORDER ON
</item>
</layer-list>
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.
编辑:我说“填充”,但在图层列表中它更像是一个偏移量。
回答by ?ükriye
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
我的问题是定义一个具有线性渐变的按钮,并用不同的颜色绘制一条顶部和一条底部的线。毕竟我破解了这个解决方案。我将文件保存在 res/drawable/blue_btn.xml 下
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" >
<shape android:shape="rectangle">
<solid android:color="@color/blue_end" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
<stroke
android:width="1dp"
android:color="@color/bottomline_btn" />
</shape>
</item>
<item>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle">
<solid android:color="@color/blue" />
<gradient
android:startColor="@color/blue"
android:endColor="@color/blue_end"
android:angle="270" />
</shape>
</item>
<item android:top="0dp" android:bottom="-1dp" android:left="-1dp" android:right="-1dp">
<shape android:shape="rectangle">
<stroke
android:width="1dp"
android:color="@color/topline_btn" />
<solid android:color="#00000000" />
<corners android:radius="0dp" />
</shape>
</item>
<item android:top="-1dp" android:bottom="0dp" android:left="-1dp" android:right="-1dp">
<shape android:shape="rectangle">
<stroke
android:width="1dp"
android:color="@color/bottomline_btn" />
<solid android:color="#00000000" />
<corners android:radius="0dp" />
</shape>
</item>
</layer-list>
</item>
</selector>
<color name="topline_btn">#31ffffff</color>
<color name="bottomline_btn">#31000000</color>
<color name="blue">#449def</color>
<color name="blue_end">#2f6699</color>
回答by Bo.
回答by Mickey Tin
My solution is to add 9-patch as the last layer item :
我的解决方案是添加 9-patch 作为最后一层项目:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape>
<solid android:color="@color/tab_button_active_layer2" />
</shape>
</item>
<item android:bottom="@dimen/tab_button_active_bottom_bar_width">
<shape>
<solid android:color="@color/tab_button_active_layer1" />
</shape>
</item>
<!-- 9-patch drawable -->
<item android:drawable="@drawable/tab_button_border_top"/>
</layer-list>