【问题标题】:Android margin between buttons in grid layout网格布局中按钮之间的 Android 边距
【发布时间】:2014-01-31 05:24:58
【问题描述】:

我正在尝试创建一个包含按钮的网格布局,但默认情况下,这些按钮之间有一个空格,我不需要它。 .xml 文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center">
    <HorizontalScrollView android:id="@+id/HorizontalScrollView"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:layout_gravity="center">
    <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/gridLayout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center">
    </GridLayout>
  </HorizontalScrollView>
</ScrollView>

我创建按钮并将其添加到网格布局的代码:

for (int rowCounter = 0; rowCounter < DIMENSION; rowCounter++)
    for (int columnCounter = 0; columnCounter < DIMENSION; columnCounter++) {
        Button b = new Button(this);
        b.setText(" ");
        GridLayout.LayoutParams params = new GridLayout.LayoutParams();
        params.setMargins(0, 0, 0, 0);
        b.setLayoutParams(params);
        gridLayout.addView(b);
    }

这是它的外观图片:

【问题讨论】:

  • 使用hierarchyviewer查看Buttons之间是否真的有边距

标签: android button margin space grid-layout


【解决方案1】:

Android 默认按钮有一些填充。
如果你不想要那个空间,你需要为你的按钮创建一个自定义背景。

这是一个例子:

button_dark_gradient.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" >
         <shape>
            <gradient
                android:startColor="#00000000"
                android:centerColor="#FFFFFF"
                android:endColor="#00000000"
                android:angle="270" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />          
        </shape>

        <shape>
            <gradient
                android:startColor="#00CFCCCE"
                android:centerColor="#FFFFFF"
                android:endColor="#00BAB8B9"
                android:angle="270" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />            
        </shape>        
    </item>
    <item>
        <shape>
            <gradient
                android:startColor="#00CFCCCE"
                android:centerColor="#FFFFFF"
                android:endColor="#00BAB8B9"
                android:angle="270" />
        </shape>
    </item>

</selector>  

将按钮的背景设置为此可绘制对象。

b.setBackground(getResources().getDrawable(R.drawable.button_dark_gradient));

【讨论】:

  • 谢谢您,先生,这就是解决方案,我在颜色和渐变上玩了一点,并将填充设置为 0,看起来不错