大家好,
我从API获得以下字段“Name”和“Skoda”。会有x个这样的项目。根据设计,我应该在下图中显示它们。
所以,我决定以一种名为“childLayout”的线性布局以编程方式创建两个textview,如下所示。
-- RelativeLayout
-- Linear Layout
-- TextView Textview --
-- Linear Layout
-- Linear Layout
-- TextView Textview --
-- Linear Layout
-- Linear Layout
-- TextView Textview --
-- Linear Layout
--RelativeLayout
但我没有得到理想的输出。请帮我解决这个问题。
这是代码:
TextView mType;
TextView mValue;
for (int i = 0; i < getDetailedDescAL.size(); i++) {
LinearLayout childLayout = new LinearLayout(
DetailedCategories.this);
LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
childLayout.setLayoutParams(linearParams);
mType = new TextView(DetailedCategories.this);
mValue = new TextView(DetailedCategories.this);
mType.setLayoutParams(new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 1f));
mValue.setLayoutParams(new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 1f));
mType.setTextSize(17);
mType.setPadding(5, 3, 0, 3);
mType.setTypeface(Typeface.DEFAULT_BOLD);
mType.setGravity(Gravity.LEFT | Gravity.CENTER);
mValue.setTextSize(16);
mValue.setPadding(5, 3, 0, 3);
mValue.setTypeface(null, Typeface.ITALIC);
mValue.setGravity(Gravity.LEFT | Gravity.CENTER);
mType.setText(getDetailedDescAL.get(i).getmPropertyType());
mValue.setText(getDetailedDescAL.get(i).getmPropertyValue());
childLayout.addView(mValue, 0);
childLayout.addView(mType, 0);
RelativeLayout.LayoutParams relativeParams =
new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
relativeParams.addRule(RelativeLayout.BELOW);
Details.addView(childLayout, relativeParams);
// Details is the relative layout declared in XML
}
输出结果为:
看起来文字视图是最重要的。如何解决这个问题。
答案 0 :(得分:3)
替换RelativeLayout
的{{1}}并将所有LinearLayout
添加到该TextView's
。
别忘了android:orientation="vertical"
LinearLayout
答案 1 :(得分:1)
for(int j=0;j<30;j++)
{
LinearLayout childLayout = new LinearLayout(
MainActivity.this);
LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
childLayout.setLayoutParams(linearParams);
TextView mType = new TextView(MainActivity.this);
TextView mValue = new TextView(MainActivity.this);
mType.setLayoutParams(new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 1f));
mValue.setLayoutParams(new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 1f));
mType.setTextSize(17);
mType.setPadding(5, 3, 0, 3);
mType.setTypeface(Typeface.DEFAULT_BOLD);
mType.setGravity(Gravity.LEFT | Gravity.CENTER);
mValue.setTextSize(16);
mValue.setPadding(5, 3, 0, 3);
mValue.setTypeface(null, Typeface.ITALIC);
mValue.setGravity(Gravity.LEFT | Gravity.CENTER);
mType.setText("111");
mValue.setText("111");
childLayout.addView(mValue, 0);
childLayout.addView(mType, 0);
linear.addView(childLayout);
}
答案 2 :(得分:0)
将相对布局更改为线性布局并已解决
答案 3 :(得分:0)
JFI,你为什么不简单地使用ListView。在帖子中,您提到项目数量未知。因此,可能存在这样的情况:您将拥有的项目数量超过可见屏幕空间的数量。 Listview将为您处理滚动。
P.S。:在这里,使用scrollView
而不是Relative或LinearLayout