【问题标题】:Dynamic views not displaying in fragment: android动态视图未显示在片段中:android
【发布时间】:2017-06-01 01:09:26
【问题描述】:

我有一个带有子 LinearLayout 的 ScrollView。我通过单独的 View 方法以编程方式将 TextViews 和 NumberPickers 添加到 LinearLayout。但是,当单击包含 ScrollView 的选项卡时,不会显示动态对象。

这是我的代码:

@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;
}

我在这里做错了什么?

【问题讨论】:

标签: android android-layout android-fragments layout-inflater


【解决方案1】:

你正在使用片段,你必须参考片段根视图来查找视图,你下面的语句什么也不做,我会建议你通过 android 基本组件

 rootview.findViewById(R.id.scroll_config);

要解决问题,请执行以下操作

public View viewFunds(View rootview) {

  ScrollView scrollView = (ScrollView)rootview.findViewById(R.id.scroll_config);//considering scroll_config is scroll view in xml
  LinearLayout linearLayout =(LinearLayout)rootview.findViewById(R.id.ll_config);//considering ll_config is LinearLayout  in xml

  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);///chek in which layou you want to add numberPicker

  }
}

在 oncreateview 中

@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());

  viewFunds(rootview);
  return rootview;
}

也经过安卓培训https://developer.android.com/training/index.html

【讨论】:

  • 对不起,我不应该包含 rootview.findViewById(R.id.scroll_config); 行,我之前正在尝试。我已经根据您的上述建议更新了代码,但仍然没有显示 linearLayout 上的动态视图。我更新了上面的 VewFunds 方法以包含我之前忘记包含的返回行。请检查上面的更新代码。谢谢
  • 是的,它奏效了。现在显示对象。非常感谢!我只是忘记根据您的代码更新这些行:ScrollView scrollView = (ScrollView)rootview.findViewById(R.id.scroll_config);LinearLayout linearLayout =(LinearLayout)rootview.findViewById(R.id.ll_config);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-11
  • 2013-03-15
相关资源
最近更新 更多