如何以编程的方式在线性布局中添加两个TextView?

6

在此输入图片描述

大家好,

我从API中获取以下字段“Name”和“Skoda”。会有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

}

输出结果为: enter image description here 看起来是文本视图互相覆盖了。如何解决?

尝试将您的视图添加到垂直方向的LinearLayout中,而不是RelativeLayout中。 - Damien R.
4个回答

3

RelativeLayout替换为LinearLayout,并将所有的TextView添加到其中。 别忘了在LinearLayout中加入android:orientation="vertical"


谢谢,它很好地工作了。我只是将父布局更改为线性布局。 :-) - Rethinavel

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);
            }

0
将你的相对布局更改为线性布局,问题就解决了。

谢谢,它运行得很好。我只是将父布局更改为线性布局,并添加了android:orientation="vertical"。 :-) - Rethinavel

0
JFI,为什么不直接使用ListView呢?在您的帖子中,您提到了项目数量是未知的。因此可能会出现这样的情况,您将拥有比可见屏幕空间更多的项目数量。ListView将为您处理滚动。
附注:在这里,使用scrollView而不是Relative或LinearLayout。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接