我有一個帶子LinearLayout的ScrollView。我通過一個單獨的View方法以編程方式將TextViews和NumberPickers添加到LinearLayout。但是,當單擊包含ScrollView的選項卡時,動態對象不會顯示。不顯示在片段中的動態視圖:android
這裏是我的代碼:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.activity_settings_tab, container, false);
super.onCreate(savedInstanceState);
myDb = new DatabaseHelper(getContext());
rootview.findViewById(R.id.scroll_config);
viewFunds();
return rootview;
}
這裏是單獨的方法我已經提到,動態地添加對象到的LinearLayout:
public View viewFunds(View rootview) {
ScrollView scrollView = new ScrollView(getActivity());
scrollView.findViewById(R.id.scroll_config);
LinearLayout linearLayout = new LinearLayout(getActivity());
linearLayout.findViewById(R.id.ll_config);
//final View linearLayout = getActivity().findViewById(R.id.ll_config);
linearLayout.removeAllViews(); //clear layout first - LINE WITH ISSUE
linearLayout.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams lp3 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//create dynamic objects inside scrollview and dynamic linear layout - horizontal
for (int i = 0; i < res2.getCount(); i++) {
LinearLayout llh = new LinearLayout(getActivity());
llh.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams lp_llh = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
llh.setLayoutParams(lp_llh);
linearLayout.addView(llh);
NumberPicker numberPicker = new NumberPicker(getActivity());
numberPicker.setMinValue(0);
numberPicker.setMaxValue(100);
LinearLayout.LayoutParams lp_np = new LinearLayout.LayoutParams(70, LinearLayout.LayoutParams.WRAP_CONTENT);
numberPicker.setLayoutParams(lp_np);
numberPicker.setGravity(Gravity.CENTER_HORIZONTAL);
//showMessage("value",res2.getString(3));
numberPicker.setValue(Integer.parseInt(res2.getString(2))); //
TextView textView = new TextView(getActivity());
textView.setText(/*textArray[i] + " " +*/ res2.getString(1));
llh.addView(textView);
linearLayout.addView(numberPicker);
}
return scrollView;
}
我在這裏做錯了什麼?
你這樣做完全錯誤檢查這個示例如何添加視圖運行時間https://stackoverflow.com/questions/31047502/how-to-add-view-in-a-linear-layout-dynamically – Pavan
這是在使用具有動態視圖的片段的上下文中。你提到的例子與它沒有關係。 –