下拉菜单Spinner的效果如下图
布局文件
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="对齐方式"/>
<Spinner
android:id="@+id/sp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
代码
private String[] align= {"上对齐","下对齐","左对齐","右对齐"};
private Spinner sp;
private ArrayAdapter<String> adapter;
private void setupView(){
sp =(Spinner) findViewById(R.id.sp);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, align);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);//定义下拉菜单里的样式
sp.setAdapter(adapter);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupView();
}