2013-02-23 62 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"