如何以编程方式创建文本视图并显示为网格
How to programmatically create textviews and show as grid
Create a new TextView programmatically then display it below another TextView
这是我试过的方法,但没有用。
我需要以编程方式创建一些文本视图,然后将它们显示为网格
第一行:第一个 Textview 第二个 Textview
第二行:第三个文本视图第四个文本视图
我正在设置列表中的文本。
基于链接的问题,这对您有用
String[] textArray = {"One", "Two", "Three", "Four","Five","Six"};
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
for( int i = 0; i < textArray.length; i+=2 )
{
LinearLayout linearLayout1 = new LinearLayout(this);
linearLayout1.setOrientation(LinearLayout.HORIZONTAL);
TextView textView1 = new TextView(this);
textView1.setText(textArray[i]);
TextView textView2 = new TextView(this);
textView2.setText(textArray[i+1]);
linearLayout1.addView(textView1);
linearLayout1.addView(textView2);
linearLayout.addView(linearLayout1);
}
<mainLayout>.addView(linearLayout);
此外,您还可以添加 LinearLayout.LayoutParams
来设置您喜欢的内容。如果你想将它居中,那么你可以使用 weights
Create a new TextView programmatically then display it below another TextView
这是我试过的方法,但没有用。
我需要以编程方式创建一些文本视图,然后将它们显示为网格
第一行:第一个 Textview 第二个 Textview 第二行:第三个文本视图第四个文本视图
我正在设置列表中的文本。
基于链接的问题,这对您有用
String[] textArray = {"One", "Two", "Three", "Four","Five","Six"};
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
for( int i = 0; i < textArray.length; i+=2 )
{
LinearLayout linearLayout1 = new LinearLayout(this);
linearLayout1.setOrientation(LinearLayout.HORIZONTAL);
TextView textView1 = new TextView(this);
textView1.setText(textArray[i]);
TextView textView2 = new TextView(this);
textView2.setText(textArray[i+1]);
linearLayout1.addView(textView1);
linearLayout1.addView(textView2);
linearLayout.addView(linearLayout1);
}
<mainLayout>.addView(linearLayout);
此外,您还可以添加 LinearLayout.LayoutParams
来设置您喜欢的内容。如果你想将它居中,那么你可以使用 weights