我有一个动态加载视图的问题。我有三个动态微调器视图,当我从微调器中选择第一个项目时,它应该在第二个微调器上方创建一个文本视图,但它在第三个微调器下创建一个视图。任何人都可以指导我如何实现它。我已经在下面分享了我的代码。
/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) {
}
});
答案 0 :(得分:0)
你知道LinearLayout的工作原理吗? http://developer.android.com/reference/android/widget/LinearLayout.html 你应该打电话给
addView(tv, 1);
指定插入视图的位置。