动态添加和删除微调器

时间:2013-11-28 05:44:14

标签: java android android-spinner removechild

您好我已经创建了一个包含两个按钮添加和删除的布局,当我点击加号按钮时,它将以编程方式添加一个微调器,我有另一个按钮(删除) - 符号,如图所示,现在我想当我点击删除按钮它将从最后删除微调器,请指导我如何删除微调器?我已经以编程方式添加了微调器,它正在以编程方式创建微调器。我会发布我的代码。 enter image description here

public class AddMenulist extends Fragment{

    TextView tv1,tv2,tv3,tv4;
    Button bt1 , bt2;
    LinearLayout linearlayout,linearlayout1;

    ArrayList<String> spinnerArray = new ArrayList<String>();
     private Spinner spinner;
     private Spinner spinner2;

    public AddMenulist(){

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_pages, container, false);

        tv1 = (TextView)rootView.findViewById(R.id.userName);
        tv2 = (TextView)rootView.findViewById(R.id.userName2);

        bt1 = (Button)rootView.findViewById(R.id.add);
        bt2 = (Button)rootView.findViewById(R.id.delete);

        linearlayout = (LinearLayout)rootView.findViewById(R.id.main_layout);

        spinnerArray.add("one");
        spinnerArray.add("two");
        spinnerArray.add("three");
        spinnerArray.add("four");
        spinnerArray.add("five");

       bt1.setOnClickListener(new View.OnClickListener()
       {




            @Override
            public void onClick(View view)
            {

                //Toast.makeText(getActivity(), "Test", Toast.LENGTH_LONG).show();

                linearlayout1 = new LinearLayout(getActivity());
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                linearlayout1.setOrientation(LinearLayout.HORIZONTAL);
                layoutParams.setMargins(10, 0, 0, 0);
                linearlayout1.setLayoutParams(layoutParams );


                tv3 = new TextView(getActivity());
                tv3.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
                tv3.setText("Label");

                tv4 = new TextView(getActivity());
                tv4.setText("Category");




                spinner = new Spinner(getActivity());
                ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item, spinnerArray);

                spinner.setAdapter(spinnerArrayAdapter);

                spinner2 = new Spinner(getActivity());
                ArrayAdapter<String> spinnerArrayAdapter2 = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item, spinnerArray);

                spinner2.setAdapter(spinnerArrayAdapter2);

                linearlayout1.addView(tv3);
                linearlayout1.addView(spinner);

                linearlayout1.addView(tv4);
                linearlayout1.addView(spinner2);
                linearlayout.addView(linearlayout1);


            }
        });

       bt2.setOnClickListener(new View.OnClickListener()
       {
            @Override
            public void onClick(View view)
            {

                //Toast.makeText(getActivity(), "Test", Toast.LENGTH_LONG).show();


            }
        });


        return rootView;
    }


}

1 个答案:

答案 0 :(得分:0)

使用它 yourSpinner.setVisibility(View.GONE);