我想有一个2x2网格内的按钮。这只是ICS,所以我试图使用新的GridLayout给定。

这是我的布局的XML:

 <?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/favorites_grid"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00ff00"
    android:rowCount="2"
    android:columnCount="2">
  <Button
      android:text="Cell 0"
      android:layout_row="0"
      android:layout_column="0"
      android:textSize="14dip" />
  <Button
      android:text="Cell 1"
      android:layout_row="0"
      android:layout_column="1"
      android:textSize="14dip" />

  <Button
      android:text="Cell 2"
      android:layout_row="1"
      android:layout_column="0"
      android:textSize="14dip" />
  <Button
      android:text="Cell 3"
      android:layout_row="1"
      android:layout_column="1"
      android:textSize="14dip" />
</GridLayout>

问题是我的视图没有为每一行均匀地拉伸。这导致GridLayout的右侧有很多额外的空间。

我尝试设置layout_gravity="fill_horizontal",但这只适用于该行的最后一个视图。这意味着单元格1会一直延伸,为单元格0提供足够的空间。

如何解决这个问题?


当前回答

我只是在网格视图中做了简单的网格布局,并在想为什么我以前没有这样做。它很容易实现。(我在示例中使用自定义包装器CSView,但只是将其更改为View)。Ops要求只适用于GridView,所以我把它贴在这里作为答案。为了使拉伸均匀,以numColumns:auto_fit android:numColumns=2为例。

用法:

CSGridLayout(this, R.id.sound_store_grid, SubscriptionUpgradeStoreDialogItem(this),
            PresetUpgradeStoreDialogItem(this), MidiUpgradeStoreDialogItem(this))

实现:

class CSGridLayout(parent: CSActivityView<*>,
                   viewId: Int,
                   vararg items: CSView<*>) : CSView<GridView>(parent, viewId) {

    private val items = list(items)
    private var adapter = Adapter()

    init {
        view.adapter = adapter
        adapter.notifyDataSetChanged()
    }

    inner class Adapter : BaseAdapter() {
        override fun getCount() = items.size
        override fun getViewTypeCount() = 1
        override fun isEnabled(position: Int) = true
        override fun getItem(position: Int) = items[position]
        override fun getItemViewType(position: Int) = 0
        override fun getItemId(position: Int) = position.toLong()
        override fun getView(position: Int, toReuseView: View?, parent: ViewGroup) =
            items[position].view
    }
}

其他回答

给你:

Button button = new Button(this);
// weight = 1f , gravity = GridLayout.FILL 
GridLayout.LayoutParams param= new GridLayout.LayoutParams(GridLayout.spec(
            GridLayout.UNDEFINED,GridLayout.FILL,1f),
            GridLayout.spec(GridLayout.UNDEFINED,GridLayout.FILL,1f));
// Layout_height = 0 ,Layout_weight = 0
params.height =0;                                                                                                           
params.width = 0;
button.setLayoutParams(param);

结果:

试试这样做:

    final int MAX_COLUMN = gridView.getColumnCount(); //5
    final int MAX_ROW = gridView.getRowCount(); //7
    final int itemsCount = MAX_ROW * MAX_COLUMN; //35

    int row = 0, column = 0;

    for (int i = 0; i < itemsCount; i++) {
        ImageView view = new ImageView(this);

        //Just to provide alternate colors
        if (i % 2 == 0) {
            view.setBackgroundColor(Color.RED);
        } else {
            view.setBackgroundColor(Color.GREEN);
        }

        GridLayout.LayoutParams params = new GridLayout.LayoutParams(GridLayout.spec(row, 1F), GridLayout.spec(column, 1F));
        view.setLayoutParams(params);
        gridView.addView(view);

        column++;

        if (column >= MAX_COLUMN) {
            column = 0;
            row++;
        }
    }

如果你想要单元格的特定宽度和高度,那么使用:

     params.width = 100; // Your width
     params.height = 100; //your height

尝试将以下内容添加到GridLayout规范中。这应该可以工作。

android:useDefaultMargins="true"

我终于找到了解决办法。就像Rotemmiz说的,你必须在之后动态地做。这段代码拉伸按钮以水平填充视图,但同样可以用于垂直填充视图。

public void fillview(android.support.v7.widget.GridLayout gl)
{
    Button buttontemp;

    //Stretch buttons
    int idealChildWidth = (int) ((gl.getWidth()-20*gl.getColumnCount())/gl.getColumnCount());
    for( int i=0; i< gl.getChildCount();i++)
    {
        buttontemp = (Button) gl.getChildAt(i);
        buttontemp.setWidth(idealChildWidth);
    }
}

(20是内部和外部的填充和空白。这可以更普遍地做,但这个要干净得多)

然后可以这样调用:

    android.support.v7.widget.GridLayout gl = (android.support.v7.widget.GridLayout)findViewById(R.id.buttongrid);
    ViewTreeObserver vto = gl.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {@Override public void onGlobalLayout() 
    {

            android.support.v7.widget.GridLayout gl = (android.support.v7.widget.GridLayout) findViewById(R.id.buttongrid);
            fillview(gl);

            ViewTreeObserver obs = gl.getViewTreeObserver();
            obs.removeGlobalOnLayoutListener(this);
    }});

它必须用一个观察者来完成,因为我们需要在调用视图之前等待视图被绘制。

这可以通过拥有相等的layout_columnWeight和layout_rowWeight来实现

布局:

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:grid="http://schemas.android.com/apk/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="3">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_rowWeight="1"
        android:layout_columnWeight="1"
        android:gravity="center"
        android:text="item 0x0"
        grid:layout_column="0"
        grid:layout_gravity="center"
        grid:layout_row="0" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_rowWeight="1"
        android:layout_columnWeight="1"
        android:gravity="center"
        android:text="item 0x1"
        grid:layout_column="1"
        grid:layout_gravity="center"
        grid:layout_row="0" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_rowWeight="1"
        android:layout_columnWeight="1"
        android:gravity="center"
        android:text="item 0x2"
        grid:layout_column="2"
        grid:layout_gravity="center"
        grid:layout_row="0" />


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_rowWeight="1"
        android:layout_columnWeight="1"
        android:gravity="center"
        android:text="item 1x0"
        grid:layout_column="0"
        grid:layout_gravity="center"
        grid:layout_row="1" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_rowWeight="1"
        android:layout_columnWeight="1"
        android:gravity="center"
        android:text="item 1x1"
        grid:layout_column="1"
        grid:layout_gravity="center"
        grid:layout_row="1" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_rowWeight="1"
        android:layout_columnWeight="1"
        android:gravity="center"
        android:text="item 1x2"
        grid:layout_column="2"
        grid:layout_gravity="center"
        grid:layout_row="1" />


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_rowWeight="1"
        android:layout_columnWeight="1"
        android:gravity="center"
        android:text="item 2x0"
        grid:layout_column="0"
        grid:layout_gravity="center"
        grid:layout_row="2" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_rowWeight="1"
        android:layout_columnWeight="1"
        android:gravity="center"
        android:text="item 2x1"
        grid:layout_column="1"
        grid:layout_gravity="center"
        grid:layout_row="2" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_rowWeight="1"
        android:layout_columnWeight="1"
        android:gravity="center"
        android:text="item 2x2"
        grid:layout_column="2"
        grid:layout_gravity="center"
        grid:layout_row="2" />


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_rowWeight="1"
        android:layout_columnWeight="1"
        android:gravity="center"
        android:text="item 3x0"
        grid:layout_column="0"
        grid:layout_gravity="center"
        grid:layout_row="3" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_rowWeight="1"
        android:layout_columnWeight="1"
        android:gravity="center"
        android:text="item 3x1"
        grid:layout_column="1"
        grid:layout_gravity="center"
        grid:layout_row="3" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_rowWeight="1"
        android:layout_columnWeight="1"
        android:gravity="center"
        android:text="item 3x2"
        grid:layout_column="2"
        grid:layout_gravity="center"
        grid:layout_row="3" />

</GridLayout>

结果