动态创建的视图无法正确显示在GridLayout中

时间:2016-07-20 10:53:39

标签: java android grid-layout android-gridlayout

我有一个GridLayout,我希望在其中显示动态生成的TextViews。至少GridLayout可以有2列,最多可以有3列。

我有一个名为decide(int)的函数,它将一个整数参数传递给另一个实际生成TextViews的函数。然后测试此整数值以确定GridLayout可以具有的列数和行数。

以下是我的 MainActivity.java

package accordiontry.juspay.accordiontry;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
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.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.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 FixedCol3 = 3, FixedCol2 = 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();

        handleClick();

        decide(6);
    }

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

    private void init()
    {
        gridLayout = (GridLayout) findViewById(R.id.gridLayout);
    }

    public void handleClick()
    {

    }

    private void decide(int num)
    {
        if((num%3)==0 || (num%2)==0)
            generateView(num);
        else
        {
            num = num -1;
            generateView(num);
        }

    }

    public void generateView(int num)
    {
        if(((num%3)==0 && (num%2)==0) || (num%3)==0)
        {
            Toast.makeText(MainActivity.this,"3 has been executed", Toast.LENGTH_SHORT).show();
            int number = num/3;
            if(number>1)
            {
                gridLayout.setRowCount(number);
                gridLayout.setColumnCount(FixedCol3);
                createView(number*FixedCol3);
            }
            else
            {
                gridLayout.setRowCount(1);
                gridLayout.setColumnCount(FixedCol3);
                createView(number*FixedCol3);
            }

        }
        else
        {
            Toast.makeText(MainActivity.this,"2 has been executed", Toast.LENGTH_SHORT).show();
            int number = num/2;
            if(number>1)
            {
                gridLayout.setRowCount(number);
                gridLayout.setColumnCount(FixedCol2);
                createView(number*FixedCol2);
            }
            else
            {
                gridLayout.setRowCount(1);
                gridLayout.setColumnCount(FixedCol2);
                createView(number*FixedCol2);
            }
        }
    }

    public void createView(int n)
    {
        for(int i=0;i<n;i++)
        {
            TextView tv = new TextView(this);
            GridLayout.LayoutParams params = new GridLayout.LayoutParams();
            params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            params.width = GridLayout.LayoutParams.MATCH_PARENT;
            tv.setLayoutParams(params);
            tv.setText("HEY THERE!!!");
            gridLayout.addView(tv);

        }
    }


}

以下是我的 content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">


    <GridLayout
        android:id="@+id/gridLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="129dp">


    </GridLayout>
</RelativeLayout>

这是我得到的输出: -

enter image description here

我的应用程序应该生成6 嘿那里!! TextViews,但它最终只生成2。

我哪里错了?

感谢您的时间!!

2 个答案:

答案 0 :(得分:0)

将gridlayout的高度设为wrap_content。

<GridLayout
    android:id="@+id/gridLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="129dp">


</GridLayout>

答案 1 :(得分:0)

自己找到解决方案。

更改

params.width = GridLayout.LayoutParams.MATCH_PARENT;

params.width = ViewGroup.LayoutParams.WRAP_CONTENT;

做了这个伎俩。