无法删除然后以编程方式将视图添加到FrameLayout

时间:2015-05-04 15:59:05

标签: java android android-layout android-view

我无法从FrameLayout中删除LinearLayout,然后添加一个以编程方式生成的EditText填充的新LinearLayout。

第一次加载视图时,我使用findViewById()捕获对FrameLayout的引用,然后以编程方式创建一个新的LinearLayout并向其添加一些EditText。 看起来像这样

formPlace = (FrameLayout) findViewById(R.id.form_place);

LinearLayout linearView = new LinearLayout(this);
linearView = makeEditText(arrayItems,category);
linearView.setBackgroundColor(Color.GRAY);
linearView.setOrientation(LinearLayout.VERTICAL);

formPlace.addView(linearView);

接下来是编辑EditTexts的函数:

private LinearLayout makeEditText(ArrayList<String[]> arrayString, int position){
        LinearLayout view = new LinearLayout(this);
        arrayInputs = new ArrayList<>();
        String[] arrayHints = arrayString.get(position);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        for (int i=0;i<arrayHints.length;i++){
            EditText editText = new EditText(this);
            Log.i("myApp","tantas veces" + i);

            editText.setId(i);
            editText.setTextColor(Color.BLACK);
            editText.setHint(arrayHints[i]);
            editText.setHintTextColor(Color.GRAY);


            editText.setLayoutParams(params);

            view.addView(editText);
            arrayInputs.add(editText);

        }
        LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        buttonParams.gravity = Gravity.RIGHT;

        android.widget.Button save = new android.widget.Button(this);
        save.setText("Guardar");
        save.setLayoutParams(buttonParams);
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("myApp", "guardar");
                saveClicked(category);
            }
        });

        view.addView(save);

        android.widget.Button cancel = new android.widget.Button(this);
        cancel.setText("cancelar");
        cancel.setLayoutParams(buttonParams);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("myApp", "cancelar");
                cancelClicked();
            }
        });

        view.addView(cancel);
        return view;
    }

然后,当调用一个微调器事件时,我会捕获FrameLayout的引用并传递.removeAllViews()。这可以按预期工作,并删除我的视图。当我尝试在makeEditText行中向FrameLayout添加新视图时出现问题:

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, final int position, long id) {

                if(firstTime==false) {
                    FormActivity.this.formPlace.removeAllViews();

                    LinearLayout auxLinearView = new LinearLayout(FormActivity.this);
                    auxLinearView = makeEditText(arrayItems, position);
                    FormActivity.this.formPlace.addView(auxLinearView);
                }
                firstTime =false;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }

        }); 

发生的情况是旧视图被删除但新视图设置不正确。它只显示4中的1个元素用于显示,而我以编程方式创建的按钮根本不显示。而我的FrameLayout占用了我屏幕上的所有剩余空间,即使设置为包装内容。它意味着视图的行为不应该如此。

编辑:

以下是一些截图

以下是removeAllViews()之前的样子:

https://dl.pushbulletusercontent.com/tExAXJexe4BfG5sGuKvb1RFtGOFd51nE/Screenshot_2015-05-04-17-54-18.png

以下是将视图添加到FrameLayout后的样子:

https://dl.pushbulletusercontent.com/JM72D3ZrLwTh0YC9g7PODREyOfCj667P/Screenshot_2015-05-04-17-54-25.png

0 个答案:

没有答案