Java TextView 动态添加到 GridLayout

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23695210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 00:45:12  来源:igfitidea点击:

TextView add to GridLayout dynamically

javaandroiduser-interface

提问by just

I have a problem, that i dont know how can i add a textview to a gridlayout. I have an xml:

我有一个问题,我不知道如何将文本视图添加到网格布局。我有一个 xml:

<TextView
        android:id="@+id/textView1"
        android:layout_column="2"
        android:layout_columnSpan="3"
        android:layout_gravity="center_horizontal|top"
        android:layout_row="3"
        android:layout_rowSpan="2"
        android:text="TextView" />

And would like to do this xml code dynamically. How can i do this? I know how to create textview, but i dont know how to add to the gridlayout...

并想动态地做这个xml代码。我怎样才能做到这一点?我知道如何创建 textview,但我不知道如何添加到 gridlayout...

采纳答案by JamieB

GridLayout gv = (GridLayout) findViewById(R.id.gridlayout);
TextView tv = new TextView(context);
tv.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP);
tv.setText("TextView);
LayoutParams params = new LayoutParams(Set your column and row information as params);
tv.setLayoutParams(params);
gv.addView(tv);

You can't paste the code in and it will need to be tested and changed to fit (it won't just work). But it should give you the general idea.

您无法粘贴代码,需要对其进行测试和更改以使其适合(它不仅可以正常工作)。但它应该给你一般的想法。