android – 如何以编程方式在线性布局中添加2 textview

嗨,大家好,

我从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,1f));

    mType.setTextSize(17);
    mType.setPadding(5,3,3);
    mType.setTypeface(Typeface.DEFAULT_BOLD);
    mType.setGravity(Gravity.LEFT | Gravity.CENTER);

    mValue.setTextSize(16);
    mValue.setPadding(5,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

}

输出是:

文本视图似乎是最重要的.怎么解决这个问题.

最佳答案
替换LinearLayout的RelativeLayout并将所有TextView添加到其中.
不要忘记LinearLayout中的android:orientation =“vertical”

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...