Jon*_*nes 5 android space margin button grid-layout
我正在尝试创建一个包含按钮的网格布局,但默认情况下这些按钮之间有一个空格,我不需要它..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);
    }
这是一个看起来如何的图像:
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));
| 归档时间: | 
 | 
| 查看次数: | 9068 次 | 
| 最近记录: |