网格布局中按钮之间的Android边距 [英] Android margin between buttons in grid layout

查看:146
本文介绍了网格布局中按钮之间的Android边距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to create a grid layout containing buttons but by default there is a space between these buttons and I don't need that. The .xml file looks like this:

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

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

The code where I create the buttons and add it to the grid layout:

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);
    }

以下是其外观的图片:

推荐答案

Android默认按钮具有一些填充.
如果您不需要该空间,则需要为按钮创建一个自定义背景.

Android default button have some padding.
If you don't want that space, you need create a custom backgroud for your buttons.

这是一个示例:

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>  

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

Set the backgroud of your buttons to this drawable.

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

这篇关于网格布局中按钮之间的Android边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆