2013-02-23 67 views
0

林造型使用形狀繪製一個按鈕,這將導致按鈕在擴大一點點或失去它的保證金所以它不與其它按鍵相吻合。任何想法,爲什麼:Android的按鈕形狀繪製保證金

enter image description here

這是我的佈局代碼:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/LinearLayout1" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 
<TableLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dip" 
    android:layout_weight="1" 
    android:stretchColumns="*" 
    android:weightSum="2" > 
<TableRow 
     android:id="@+id/tableRowMem" 
     android:layout_weight="1" > 

      <Button 
       android:id="@+id/buttonA" 
       android:layout_width="0dip" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:text="A"/> 

     <Button 
      android:id="@+id/buttonB" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:text="B" /> 

    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow1" 
     android:layout_weight="1" > 

     <Button 
      android:id="@+id/buttonC" 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:layout_height="match_parent" 
      android:text="C" /> 

     <Button 
      android:id="@+id/buttonD" 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:layout_height="match_parent" 
      android:background="@drawable/buttonred" 
      android:text="D" /> 
    </TableRow> 
    </TableLayout> 
    </LinearLayout> 

這是形狀繪製:

<?xml version="1.0" encoding="utf-8"?> 
<selector android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android"> 
<item> 

    <shape> 
     <gradient 
    android:startColor="#FFFF0000" 
    android:endColor="#80FF00FF" 
    android:angle="45"/> 

    </shape> 
</item> 
</selector> 

回答

0

請看下圖。該默認的按鈕是9修補圖像所以總是按鈕後額外的空間,這是你的按鈕的捕獲。因此,當您設置背景資源時,圖像會填充空白區域。所以最好用ImageView代替。它將與imageview完美配合。雖然可能有像填充這樣的解決方案,但在某些情況下它們會失敗。您可以使用ImageView實現功能。我希望這能幫到您。

Default Button

1

好像每個按鈕都有填充一些DP或佈局行跡。

嘗試設置

android:padding=0 

如果不行

android:layout_margin=0 

這將消除從默認按鈕的保證金,如果你想解決的其他方式,增加保證金定製按鈕

0

這可能是因爲其他可繪製在畫布可能有一個填充。

檢查畫布填充[不是一個你在XML設置,但設計師的圖像本身設置一個]並使用新繪製匹配它,看看他們是相同的。他們應該是一樣的。

+0

我在哪裏可以看到該值? – aviran 2013-02-23 07:40:58

+0

在如Photoshop/GIMP之類的圖像編輯器,只需打開每個圖像,檢查大小,他們可能有任何透明墊襯。 – achie 2013-02-23 11:41:57

0

我下面的佈局試試我已經做了一些相關的變化:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/LinearLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
    <TableLayout 
     android:layout_width="fill_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1" 
     android:stretchColumns="*" 
     android:weightSum="2" > 
    <TableRow 
    android:id="@+id/tableRowMem" 
    android:weightSum="1" > 
     <Button 
      android:id="@+id/buttonA" 
      android:layout_width="0dip" 
      android:layout_height="fill_parent" 
      android:layout_weight="0.5" 
      android:text="A"/> 

    <Button 
     android:id="@+id/buttonB" 
     android:layout_width="0dp" 
     android:layout_height="fill_parent" 
     android:layout_weight="0.5" 
     android:text="B" /> 

</TableRow> 

<TableRow 
    android:id="@+id/tableRow1" 
    android:weightSum="1" > 

    <Button 
     android:id="@+id/buttonC" 
     android:layout_width="0dp" 
     android:layout_weight=".5" 
     android:layout_height="fill_parent" 
     android:text="C" /> 

    <Button 
     android:id="@+id/buttonD" 
     android:layout_width="0dp" 
     android:layout_weight="0.5" 
     android:layout_height="fill_parent" 
     android:background="@drawable/buttonred" 
     android:text="D" /> 
</TableRow> 
</TableLayout> 
</LinearLayout> 
+0

佈局有點不同,但同樣,相同的問題 – aviran 2013-02-23 07:39:56

+0

給我解釋一下你怎麼想安排你的按鈕? – GrIsHu 2013-02-23 07:42:30

+0

它究竟是如何看我上傳的圖片中,我希望所有的按鈕具有相同的高度/寬度,應擴大盡可能多地。 – aviran 2013-02-23 07:49:58

0

添加填充到按鈕。

android:padding="0dp"