简体   繁体   English

如何使动态生成的按钮在Android的GridLayout中占据均匀的空间?

[英]How do I make dynamically generated buttons occupy even space in a GridLayout in Android?

I'm trying to fit four dynamically generated Buttons to occupy even space in a GridLayout. 我正在尝试适应四个动态生成的Button,以在GridLayout中占据均匀的空间。 I have tried everything I possibly can, from setting LayoutParams to getting the onMeasuredWidth , but I simple can't achieve what I want. 我已经尝试了一切可能的方法,从设置LayoutParams到获取onMeasuredWidth ,但是我简单地无法实现我想要的。

This is what I want and the following is what I end up getting 就是我想要的,以下是我最终得到的

在此处输入图片说明

I have looked into this and many others, none of them proposed a solution. 我已经研究了这个问题 ,还有许多其他问题,没有一个提出解决方案。

Following is the XML ( content.xml ) containing the GridLaayout 以下是包含GridLaayout的XML( content.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="accordiontry.juspay.accordiontry.MainActivity"
    tools:showIn="@layout/activity_main"
    android:orientation="vertical">


    <GridLayout
        android:id="@+id/gridLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:columnCount="2">

    </GridLayout>
</LinearLayout>

And following is the MainActivity.java 接下来是MainActivity.java

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.webkit.WebView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private GridLayout gridLayout;
    private final int NumOfCol = 2;
    private final int NumOfRows = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        init();
        generateView();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void init()
    {
        gridLayout = (GridLayout) findViewById(R.id.gridLayout);
        gridLayout.setBackgroundColor(Color.BLUE);
        gridLayout.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    public void generateView()
    {
        Button btn1 = new Button(this);
        Button btn2 = new Button(this);

        btn1.setText("BUTTON 1");
        btn1.setWidth(gridLayout.getMeasuredWidth());
        gridLayout.addView(btn1);

        btn2.setText("BUTTON 2");
        btn2.setWidth(gridLayout.getMeasuredWidth());
        gridLayout.addView(btn2);

    }

}

I have checked your code please do as in your xml layout 我已经检查了您的代码,请按照您的xml布局进行操作

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="accordiontry.juspay.accordiontry.MainActivity"
    tools:showIn="@layout/activity_main"
    android:orientation="vertical">


    <GridLayout
        android:id="@+id/gridLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        android:columnCount="2">

    </GridLayout>
</LinearLayout>

or set fix height gridlayout 或设置固定高度gridlayout

this is working well in my case 就我而言这很好

What do you mean by saying "dynamically"? 你说“动态地”是什么意思? Adding a button by user? 按用户添加按钮? If so may be you could add button with already known width and height and put them in places you want to AND THEN USE android:alpha="0" (or 1, sorry, don't remember, try both) and android:enabled="false" and erase all information from buttons. 如果可能的话,您可以添加具有已知宽度和高度的按钮,并将其放置在要放置的位置,然后使用android:alpha="0" (或1,对不起,不记得了,请同时尝试)和android:enabled="false"并清除按钮中的所有信息。 It will take a long time but you will perfectly know that they will be on their places. 这将花费很长时间,但是您将完全知道他们会在他们的位置上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM