app\src\main\res\layout\main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/books"
android:prompt="@string/tip"/>
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:prompt="@string/tip"/>
</LinearLayout>
app\src\main\res\values\arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="books">
<item>疯狂Java讲义</item>
<item>疯狂Ajax讲义</item>
<item>疯狂XML讲义</item>
</string-array>
</resources>
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class MainActivity extends Activity
{
Spinner spinner;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 获取界面布局文件中的Spinner组件
spinner = (Spinner) findViewById(R.id.spinner);
String[] arr = { "孙悟空", "猪八戒", "唐僧" };
// 创建ArrayAdapter对象
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, arr);
// 为Spinner设置Adapter
spinner.setAdapter(adapter);
}
}