【发布时间】:2017-09-13 01:33:00
【问题描述】:
是否可以在相对布局中动态添加 textView,从数组列表中获取信息?我会解释的。例如,我有一个从数据库中获得的带有五个单词的 arralist。然后我会在其布局中创建 5 个不同的 textView。 我不能使用列表视图,因为它是用户选择这些 TextView 的位置。
这是我正在工作的代码,因此我能够创建更多 TextView,但不能从数组列表中创建。我还需要给每个TextView的上边距赋予不同的值!
RelativeLayout rl = (RelativeLayout) findViewById(R.id.sundayRelativeLayout);
TextView iv;
RelativeLayout.LayoutParams params;
float d = this.getResources().getDisplayMetrics().density;
iv = new TextView(this);
iv.setBackgroundColor(Color.YELLOW);
int marginTopDip = 180; // margin in dips
int marginHeightDips = 60; // margin in dips
int height = (int)(marginHeightDips * d);
int margintop = (int)(marginTopDip * d);
params = new RelativeLayout.LayoutParams(500, height);
params.topMargin = margintop;
rl.addView(iv, params);
iv = new TextView(this);
iv.setBackgroundColor(Color.RED);
params = new RelativeLayout.LayoutParams(30, 40);
params.topMargin = 900;
rl.addView(iv, params);
【问题讨论】:
-
您可以通过编程方式创建 TextView 并使用
LayoutParams定位它们