Android按钮形状可绘制边距

时间:2013-02-23 06:38:47

标签: android android-layout android-button android-drawable

我使用可绘制的形状设置按钮样式,这会导致按钮过度扩展或丢失它的边距,因此它与其他按钮不能很好地对齐。任何想法为什么:

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>

5 个答案:

答案 0 :(得分:1)

似乎每个按钮都有一些填充或布局损坏的dp。

尝试设置

android:padding=0

如果不起作用

android:layout_margin=0

这将消除默认按钮的边距,如果你想解决另一种方法,为自定义按钮添加边距

答案 1 :(得分:1)

请看下图。默认按钮是9补丁图像,因此按钮后总是有额外的空间,这是捕获按钮。因此,当您设置背景资源时,图像将填充空白区域。因此,最好使用ImageView。它将与imageview完美配合。虽然可能存在像填充这样的解决方案,但它们会在某些情况下失败。您可以使用ImageView实现该功能。我希望这会对你有所帮助。

Default Button

答案 2 :(得分:0)

这可能是因为其他drawables的画布可能有填充。

检查画布填充[不是您在xml中设置的那个,而是设计者在图像中设置的那个]并将其与您的新drawable匹配,看看它们是否相同。它们应该是一样的。

答案 3 :(得分: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>

答案 4 :(得分:0)

将按钮添加到按钮。

android:padding="0dp"
相关问题