2015-08-26 76 views
-2

我有動態加載視圖的問題。當我從Spinner中選擇第一個項目時,我有三個動態微調視圖,它應該在第二個微調控件上方創建一個文本視圖,但它在第三個微調控件下面創建一個視圖。任何人都可以指導我如何實現它。我已經在下面分享了我的代碼。使用微調創建動態視圖在Android中可靠可靠

/add LayoutParams 
     LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
     rl.setOrientation(LinearLayout.VERTICAL); 


     // add spinner 

     String[] spinnerArray = {"Android","ios","other"}; 
     Spinner spinner = new Spinner(this); 
     ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spinnerArray); //selected item will look like a spinner set from XML 
     spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     spinner.setAdapter(spinnerArrayAdapter); 


     String[] spinnerArray1 = {"flowers","vegetables","other"}; 
     Spinner spinner1 = new Spinner(this); 
     ArrayAdapter<String> spinnerArrayAdapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spinnerArray1); //selected item will look like a spinner set from XML 
     spinnerArrayAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     spinner1.setAdapter(spinnerArrayAdapter1); 



     String[] spinnerArray2 = {"cat","dog","other"}; 
     Spinner spinner2 = new Spinner(this); 
     ArrayAdapter<String> spinnerArrayAdapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spinnerArray2); //selected item will look like a spinner set from XML 
     spinnerArrayAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     spinner2.setAdapter(spinnerArrayAdapter2); 


     rl.addView(spinner); 
     rl.addView(spinner1); 
     rl.addView(spinner2); 

     spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
      @Override 
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) 
      { 
      if(position==1) 
      { 
       TextView tv = new TextView(MainActivity.this); 
       tv.setText("Hello"); 
       rl.addView(tv); 
      } 
      } 

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

      } 
     }); 

回答