6

在此处输入图像描述

大家好,

我从 API 获得以下字段“名称”和“斯柯达”。将有 x 个这样的项目。根据设计,我应该如下图所示显示它们。

因此,我决定以编程方式在名为“childLayout”的线性布局中创建两个文本视图,如下所示。

-- 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

}

输出是:

在此处输入图像描述

似乎文本视图是压倒一切的。如何解决这个问题。

4

4 回答 4

3

替换RelativeLayoutfor aLinearLayout并添加所有TextView's内容。别忘了android:orientation="vertical"LinearLayout

于 2013-05-27T10:22:38.527 回答
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);
            }
于 2014-07-25T21:47:43.487 回答
0

将您的相对布局更改为线性布局并解决

于 2013-05-27T10:20:58.333 回答
0

JFI,你为什么不简单地使用 ListView。在帖子中,您提到项目数量未知。因此,在某些情况下,您将拥有比可见屏幕空间更多的项目。Listview 将为您处理滚动。

PS:这里不用Relative或者LinearLayout,使用scrollView

于 2013-05-27T10:26:36.487 回答