2009-08-15 34 views
43

我想在XML文件/佈局中聲明的<shape>中設置填充。但是無論我設置什麼,沒有任何與填充有關的變化。如果我修改任何其他屬性,我會看到UI上的效果。但它不適用於填充。您能否就可能發生這種情況的原因提供建議?填充不會影響<shape>在XML佈局

這裏是XML形狀我試圖風格:

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
     android:shape="rectangle" 
> 
    <stroke android:width="1dp" android:color="#ffffff" 
      android:dashWidth="2dp" 
    /> 

    <solid android:color="@color/button_white_cover" /> 

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

    <padding android:left="1dp" 
      android:top="20dp" 
      android:right="20dp" 
      android:bottom="2dp" /> 
</shape> 

回答

81

我終於解決了我的填充問題。

因此填充在這裏對形狀沒有影響。而不是這個,你將不得不在其他可以使用它的drawable中使用填充。所以,我有一個使用形狀的繪製,有我以下:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/shape_below"/> 
    <item android:top="10px" android:right="10px" android:drawable="@drawable/shape_cover"/> 
</layer-list> 

所以,你可以在第二<item> - 元素看,我設置填充(頂部和右側)。此後一切正常。

謝謝。

+0

嗨,我不知道怎樣應用這個問題的解決方法。我有一個使用形狀和設置adnroid的按鈕:top和android:right沒有效果。我將如何解決這個問題?謝謝。 – icecream 2010-10-18 07:08:20

+2

我想Lyubomyr說的是做這樣的事情,這對我很有用,但是角落總是有不透明的白色背景,導致顯示問題(對評論格式抱歉): \t \t \t <形狀機器人:形狀= 「矩形」> \t \t \t <大小機器人:寬度= 「0dp」 機器人:高度= 「45dp」 \t \t \t \t機器人:顏色= 「@機器人:彩色/透明」/> \t \t \t \t <項機器人:頂= 「0dp」 機器人:右= 「0dp」 機器人:底部= 「0dp」 \t \t機器人:左= 「0dp」 機器人:抽拉= 「@繪製/ button_blue_shape」/ > DustinB 2011-02-18 22:12:01

+0

同樣偏離主題,但可能是有用的:如果您的可繪製使用的只是一種顏色,您也可以像android:drawable =「@ color/divider」一樣編寫。但是這在較舊的設備上破壞了。我在2.2版本上測試過,它被破壞了。不知道它在哪個級別可以接受。 – Pijusn 2013-01-08 08:48:47

13

通過Lyobomyr答案的作品,但這裏是應用了相同的解決方案,但沒有創建一個新的繪製,從而最大限度地減少文件雜亂的開銷:

How to add padding to gradient <shape> in Android?

+0

感謝這個答案Yahel,但記住不要發佈鏈接只有答案。始終在答案中提供所有必要的信息,因爲鏈接內容可能會更改或無效。 – TheIT 2016-06-30 00:33:57

31

作爲一對夫婦的意見都提到,最好的方法是將<shape>包裝在<item>元素中,並設置填充。然而,還有其他選項可用,詳情請參閱下面的鏈接。例如

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item 
     android:left="8dp" 
     android:right="8dp" 
     android:top="8dp" 
     android:bottom="8dp"> 

     <shape android:shape="rectangle"> 
      ... 
     </shape> 
    </item> 
</layer-list> 

根據您的需求,而不是使用層列表,你可以使用下列任何可繪製類型:

  • 層列表
  • 選擇
  • 級列表
  • 轉換

欲瞭解更多信息ab出可用的繪製類型看this android documentation

來源:

+0

是的,你沒有提到'' – user924 2017-08-31 13:25:23

+0

@ user924我不確定你的意思。如果這符合你的目的,那麼可以在之內包含該項目,但是對於原始問題或傳達的一般解決方案是將中的內容與填充嵌套在一起 – TheIT 2017-08-31 23:15:17

+1

的意思是我最好給你一個例子可以複製和運行沒有版本,但在這種情況下,不會被解決(因爲它應該在內) – user924 2017-09-01 07:59:06

3

我解決了這個問題是這樣的:

<shape android:shape="oval" > 
    <stroke android:width="4dp" android:color="@android:color/transparent" /> 
    <solid android:color="@color/colorAccent" /> 
</shape> 

剛剛添加了一個透明筆畫。

0

您可以嘗試插圖:

<?xml version="1.0" encoding="UTF-8"?> 
<inset xmlns:android="http://schemas.android.com/apk/res/android" 
    android:insetBottom="2dp" 
    android:insetLeft="1dp" 
    android:insetRight="20dp" 
    android:insetTop="20dp"> 

    <shape android:shape="rectangle"> 

     <stroke 
      android:width="1dp" 
      android:color="#ffffff" 
      android:dashWidth="2dp" /> 

     <solid android:color="@color/button_white_cover" /> 

     <corners android:radius="11dp" /> 
    </shape> 

</inset>