在 Android 中使用 Spinner Dependable 创建动态视图
Posted
技术标签:
【中文标题】在 Android 中使用 Spinner Dependable 创建动态视图【英文标题】:Creating a Dynamic view with spinner Dependable in Android 【发布时间】:2015-08-26 12:53:32 【问题描述】:我遇到了动态加载视图的问题。当我从微调器中选择第一项时,我有三个动态微调器视图,它应该在第二个微调器上方创建一个文本视图,但在第三个微调器下方创建一个视图。谁能指导我如何实现它。我在下面分享了我的代码。
/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)
);
【问题讨论】:
【参考方案1】:你知道线性布局是如何工作的吗? http://developer.android.com/reference/android/widget/LinearLayout.html 你应该打电话给
addView(tv, 1);
指定插入视图的位置。
【讨论】:
以上是关于在 Android 中使用 Spinner Dependable 创建动态视图的主要内容,如果未能解决你的问题,请参考以下文章