gpt4 book ai didi

java - 动态创建的 View 无法正确显示在 GridLayout 中

转载 作者:太空宇宙 更新时间:2023-11-04 12:24:52 25 4
gpt4 key购买 nike

我有一个 GridLayout,我希望在其中显示动态生成的 TextViewsGridLayout 至少可以有 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 个。

我哪里出错了?

感谢您的宝贵时间!!

最佳答案

将网格布局的高度设为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>

关于java - 动态创建的 View 无法正确显示在 GridLayout 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38479268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com