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

前端之家收集整理的这篇文章主要介绍了android – 如何以编程方式在线性布局中添加2 textview前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

嗨,大家好,

我从API获得以下字段“Name”和“Skoda”.会有x个这样的项目.根据设计,我应该在下图中显示它们.

所以,我决定以编程方式在名为“childLayout”的线性布局中创建两个textview,如下所示.

  1. -- RelativeLayout
  2. -- Linear Layout
  3. -- TextView Textview --
  4. -- Linear Layout
  5. -- Linear Layout
  6. -- TextView Textview --
  7. -- Linear Layout
  8. -- Linear Layout
  9. -- TextView Textview --
  10. -- Linear Layout
  11. --RelativeLayout

但我没有得到理想的输出.请帮我解决这个问题.

这是代码

  1. TextView mType;
  2. TextView mValue;
  3. for (int i = 0; i < getDetailedDescAL.size(); i++) {
  4. LinearLayout childLayout = new LinearLayout(
  5. DetailedCategories.this);
  6. LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(
  7. LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
  8. childLayout.setLayoutParams(linearParams);
  9. mType = new TextView(DetailedCategories.this);
  10. mValue = new TextView(DetailedCategories.this);
  11. mType.setLayoutParams(new TableLayout.LayoutParams(
  12. LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,1f));
  13. mValue.setLayoutParams(new TableLayout.LayoutParams(
  14. LayoutParams.WRAP_CONTENT,1f));
  15. mType.setTextSize(17);
  16. mType.setPadding(5,3,3);
  17. mType.setTypeface(Typeface.DEFAULT_BOLD);
  18. mType.setGravity(Gravity.LEFT | Gravity.CENTER);
  19. mValue.setTextSize(16);
  20. mValue.setPadding(5,3);
  21. mValue.setTypeface(null,Typeface.ITALIC);
  22. mValue.setGravity(Gravity.LEFT | Gravity.CENTER);
  23. mType.setText(getDetailedDescAL.get(i).getmPropertyType());
  24. mValue.setText(getDetailedDescAL.get(i).getmPropertyValue());
  25. childLayout.addView(mValue,0);
  26. childLayout.addView(mType,0);
  27. RelativeLayout.LayoutParams relativeParams =
  28. new RelativeLayout.LayoutParams(
  29. LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
  30. relativeParams.addRule(RelativeLayout.BELOW);
  31. Details.addView(childLayout,relativeParams);
  32. // Details is the relative layout declared in XML
  33. }

输出是:

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

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

猜你在找的Android相关文章