在Android上的形状绘制上边框 [英] Drawing top border on a shape on Android

查看:188
本文介绍了在Android上的形状绘制上边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个自定义进度栏(位于下的WebView ),什么我想提请之间的的WebView 和进度。我修改现有的绘制,即 progress_horizo​​ntal.xml ,并试图像这样:

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>

你有更好的想法如何画一条线形状排列与定义的可绘制的顶层列表

推荐答案

我花了一段时间试图弄清楚这一点为好。希望有一个更好的方式来做到这一点,但这里是我使用的方法,这也是一种的hackish,但在情况下,它可以帮助别人,我想我会分享。

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.

在图层列表中的第一个项目应该是一个坚实的任何颜色,你想成为你的边界。下面这将是东西(S)你想有边界,与填充的任何方面,你想的边境上。

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.

例如:

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

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

I said "padding" but in layer-lists it's more of an offset.

这篇关于在Android上的形状绘制上边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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