为什么最后的显示 textview覆盖了spinner了?
采用androidstudio编写的
public class MainActivity extends AppCompatActivity
implements AdapterView.OnItemSelectedListener {
private TextView textView;
private Spinner spinner;
private List<String>list;
private ArrayAdapter<String>adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=(TextView) findViewById(R.id.textView);
spinner=(Spinner) findViewById(R.id.spinner);
/**
* 设置数据源
*新建arrayadapter (数组适配器)
*adapter设置一个下拉列表样式
* spinner 加载适配器
*/
list=new ArrayList<String>();
textView.setText("你选择的城市是");
list.add("北京");
list.add("上海");
list.add("深圳");
list.add("广州");
adapter=new ArrayAdapter<String>
(this,android.R.layout.simple_spinner_item,list);
adapter.setDropDownViewResource
(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
//spinner设置监听器
spinner.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String cityName=adapter.getItem(position);
//String city=list.get(position);
textView.setText("你选择的是"+cityName);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.imoc.spinner.MainActivity"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textColor="#ff0000"
android:id="@+id/textView"
/>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner"
android:gravity="center_horizontal"
>
</Spinner>
</RelativeLayout>