1

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

例如:

|文本视图1| |文本视图2|
|文本视图1| |文本视图2|
ETC..

我到目前为止的代码是:

        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。

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

4

1 回答 1

1

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

于 2013-10-21T13:40:56.187 回答