【问题标题】:Android button shape drawable marginAndroid 按钮形状可绘制边距
【发布时间】:2013-02-23 06:38:47
【问题描述】:

我正在使用可绘制的形状设置按钮的样式,这会导致按钮过度扩展一点或失去它的边距,因此它不能与其他按钮很好地对齐。知道为什么:

这是我的布局代码:

<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>

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      尝试设置

      android:padding=0
      

      如果这不起作用

      android:layout_margin=0
      

      这将消除默认按钮的边距,如果您想以其他方式解决,请为自定义按钮添加边距

      【讨论】:

        【解决方案3】:

        这可能是因为其他可绘制对象的画布可能有填充。

        检查画布填充[不是您在 xml 中设置的填充,而是设计师在图像本身中设置的填充],并将其与您的新可绘制对象匹配,看看它们是否相同。它们应该是相同的。

        【讨论】:

        • 我在哪里可以看到该值?
        • 只需在 photoshop/gimp 等图像编辑器中打开每张图像,然后检查它们的大小和任何透明填充。
        【解决方案4】:

        试试我下面的布局,我做了一些相关的改变:

         <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>
        

        【讨论】:

        • 布局有点不同,但同样的问题
        • 请解释一下您希望如何排列按钮?
        • 和我上传的图片一模一样,我希望所有按钮都具有相同的高度/宽度,并且应该尽可能地展开。
        • 你的意思是当你应用drawable时它会被扩展?
        • 是的,在布局中设置背景也会使红色按钮错位
        【解决方案5】:

        为按钮添加内边距。

        android:padding="0dp"
        

        【讨论】: