【问题标题】:TextView add to GridLayout dynamicallyTextView 动态添加到 GridLayout
【发布时间】:2021-10-06 17:09:15
【问题描述】:

我有一个问题,我不知道如何将文本视图添加到网格布局。 我有一个 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" />

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

【问题讨论】:

  • 使用addView(...)。还有this.

标签: java android user-interface


【解决方案1】:
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);

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

【讨论】:

  • 所以我想我不明白什么是gridview,什么是gridlayout..以及如何将列和行信息设置为参数?这是我不知道的
  • 哦,我看错了帖子,你可以把GridView改成GridLayout,我已经更新了我的帖子。
  • 以及如何将列和行信息设置为参数?这是我不知道的
  • 你能举一个“将你的列和行信息设置为参数”的例子吗?
猜你喜欢
  • 2012-02-06
  • 2012-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多