2010-09-09 49 views
10

我試圖繪製一個帶有邊框的圓角矩形,它使用延伸ShapeDrawable的類(請參閱here),並且一切正常,除了shaperawable似乎正在切斷某些邊界,因爲形狀本身不會超出這些範圍。ShapeDrawable內的偏移形狀

是否沒有某種方法來抵消shaperawable開始繪製的位置,以便在形狀本身和畫布的邊界之間有一些填充?我已經嘗試將ShapeDrawable.setBounds都設置爲大於Shape和ShapeDrawable.setPadding的固有尺寸,但似乎無法獲取任何位置。我應該繼承可繪製而不是?

回答

21

你可以用你塑造繪製插圖與(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:[newInsetDrawable(shape,x,x,x,x)],並通過它來解決它。 InsetDrawable似乎有點隱藏在文檔中 - 非常有幫助 – 2010-09-09 20:38:39

+2

我剛剛發現,不是傳遞android:drawable,而是可以嵌套drawable,所以你不需要兩個文件。不知道爲什麼我認爲這是不可能的。 – velis 2016-06-08 07:31:19

0

試試這在你的繪製...!

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

填充不適合我 – Gilian 2017-04-17 21:02:16