為什么最后的顯示 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>
2017-01-08
你是相對布局,要設置margin,或者是在textview在spinner的上方,