2013-11-28 78 views
0

嗨我創建了一個包含兩個按鈕添加和刪除的佈局,當我點擊加號按鈕時,它將添加一個微調器,並且我有另一個按鈕(刪除)可以在圖片中看到,現在我希望當我點擊刪除按鈕時,它會從最後刪除微調,請指導我如何刪除微調?我已經通過編程添加了微調,並且正在編程中創建微調。我會發布我的代碼。 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; 
    } 


} 
+0

您沒有嘗試刪除功能?或目前它從第一個刪除? –

+0

試試這個:'linearlayout1.removeView(view)' –

+0

好吧,我會嘗試一個。 – Achin

回答

0

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

+0

我用這個linearlayout1.removeView(view),它工作,但它只是刪除最後的spinners和兩個標籤,我想從最後一個刪除兩個spinners和兩個文本視圖,它是刪除最後兩個標籤和最後兩個紡紗廠。我已經使用這個代碼。 linearlayout1.removeView(旋轉器); linearlayout1.removeView(TV3); linearlayout1.removeView(spinner2); linearlayout1.removeView(TV4); – Achin

+0

你想做什麼?刪除前兩個紡紗器和textviews或最後一個?你是什麼意思逐一。請解釋 –

+0

我有標籤和微調再次標籤和微調水平線性佈局,你可以看到我的代碼和圖片。當我點擊刪除按鈕,我想從最後一個刪除兩個文本視圖和兩個紡紗器買一個我已經使用這個代碼,但它是刪除最後兩個文本視圖和兩個水平旋轉。 linearlayout1.removeView(spinner); \t \t linearlayout1.removeView(tv3); \t \t linearlayout1.removeView(spinner2); \t \t linearlayout1.removeView(tv4); – Achin