动态添加多个水平线性布局到选项卡

时间:2013-10-21 13:39:24

标签: java android

我有一个案例,我需要将2个TextView添加到水平LinearLayout,并复制该结构数次。

例如:

| TextView1 | | TextView2 |
| TextView1 | | TextView2 |
等。

我到目前为止的代码是:

        public View createTabContent(String tag){
            LinearLayout mainTabLayout = new LinearLayout(Result.this);
            mainTabLayout.setOrientation(LinearLayout.HORIZONTAL);
            mainTabLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

            for( Object param : mainHashMap.values() ){
                String key = param.toString();

                LinearLayout linLayout = new LinearLayout(Result.this);
                linLayout.setOrientation(LinearLayout.HORIZONTAL);

                linLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

                TextView tvKey = new TextView(Result.this);
                tvKey.setText(key);
                tvKey.setTextSize(15);
                linLayout.addView(tvKey);
                try{
                    String member = this.transformMember(key);
                    Method method = mainClass.getMethod("get" + member);

                    TextView tvValue = new TextView(Result.this);
                    tvValue.setText((method.invoke(mainData) != null) ? method.invoke(dvlaData).toString() : "");
                    tvValue.setTextSize(10);
                    linLayout.addView(tvValue);
                } catch (InvocationTargetException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                } catch (IllegalAccessException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
                Log.d("ResultActivity", "adding to view");
                mainTabLayout.addView(linLayout);
            }
            return mainTabLayout;
        }

Log.d显示它正在运行它所需的每个项目,但TabContent只显示最后一个LinearLayout。它覆盖了之前添加到mainTabLayout的LinearLayouts。

我希望这是有道理的......

1 个答案:

答案 0 :(得分:1)

尝试将mainTabLayout方向设置为垂直方向而不是水平方向