在ShapeDrawable中偏移形状

12

我正在尝试使用扩展ShapeDrawable类的类绘制一个带有边框的圆角矩形(请参见此处),除了shapedrawable似乎在剪切一些边框因为形状本身并没有超出这些边界之外,一切都运行正常。

难道没有某种方式可以偏移shapedrawable开始绘制的位置,以便形状本身的边界和画布之间存在一些填充? 我已经尝试了ShapeDrawable.setBounds大于形状的固有大小和ShapeDrawable.setPadding,但好像没有任何进展。我应该继承Drawable而不是ShapeDrawable吗?

2个回答

25

你可以将形状drawable用inset (inset_shape.xml)包装起来:

    <inset xmlns:android="http://schemas.android.com/apk/res/android"
     android:drawable="@drawable/your_shape_drawable" android:insetBottom="10dip"
     android:insetLeft="10dip" android:insetRight="10dip" android:insetTop="10dip"
     android:visible="true" />

然后根据需要使用inset_shape即可。


2
谢谢,我在代码中创建了一个InsetDrawable:[new InsetDrawable(shape,x,x,x,x)],并传递了它解决了问题。 在文档中,InsetDrawable 看起来有些隐藏,非常有帮助。 - Stev_k
2
我刚刚发现,除了传递android:drawable之外,您还可以嵌套drawable,这样您就不需要两个文件。不知道为什么我一直以为这是不可能的。 - velis

0
尝试将此代码放入drawable中...!
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <solid android:color="#e1e1e1" />

    <stroke
        android:width="2dp"
        android:color="#808080" />

    <corners android:radius="10dp" />

    <padding
        android:bottom="5dp"
        android:left="5dp"
        android:right="5dp"
        android:top="5dp" />

</shape>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接