1
我有一種情況下,我需要將2個TextViews添加到水平LinearLayout,並複製該結構數次。動態添加多個水平linearlayour選項卡
例如:
| 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。
我希望這是有道理的......
一個愚蠢的錯誤。我也注意到了。 –
適用於我們所有人哈哈。 –