在 Android 上绘制形状的顶部边框

发布于 2024-08-20 21:07:01 字数 1089 浏览 7 评论 0原文

我正在创建一个自定义进度条(位于 WebView 下),我想在 WebViewProgressBar 之间绘制一条 1dp 宽的线。我正在修改现有的可绘制对象,即 progress_horizo​​ntal.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

故事与诗 2024-08-27 21:07:01

我也花了一段时间试图弄清楚这一点。希望有更好的方法来做到这一点,但这是我使用的方法,这也有点黑客,但如果它对某人有帮助,我想我会分享。

图层列表中的第一项应该是您想要作为边框的任何颜色的纯色。接下来是您想要有边框的东西,在您想要边框的任何一侧都有填充。

例如:

<?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>

这个想法是项目上的填充显示其后面的实体形状,从而提供边框外观。您可以在任意一侧添加内边距以添加边框。我想你可以变得更复杂,并且通过这种方式也可以有不同颜色的边框。

看起来像是一个黑客,但它对我有用。

编辑:我说的是“填充”,但在图层列表中它更多的是偏移。

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:

<?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.

深海少女心 2024-08-27 21:07:01

我正在浏览所有相关主题,但没有人能解决我的问题。但其中一些对于理解图层列表或形状参数的工作原理非常有用。

我的问题是定义一个具有线性渐变的按钮并用不同的颜色绘制顶线和底线。毕竟我破解了这个解决方案。我将文件保存在 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>

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

    <?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>
我家小可爱 2024-08-27 21:07:01

你有没有尝试用这样的东西来抵消它

http://android.amberfog.com/?p=9

Did you try offsetting it by something like this

http://android.amberfog.com/?p=9

鹿港巷口少年归 2024-08-27 21:07:01

我的解决方案是添加 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>

My solution is to add 9-patch as the last layer item :

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文