編輯:Android開發教程
從前面的幾節課可知,ListView用來顯示一個長列表信息,同時把整個屏幕占滿了(ListActivity)。但 是有的時候,你可能需要其他類似的視圖,這樣,你就不必把整個屏幕都占滿了。在這種情況下,你就應該 使用Spinner控件。Spinner一次顯示列表中的一個信息,並且它能讓用戶進行選擇。
下面將展示如何 在Activity中使用Spinner。
1. 創建一個工程:BasicViews6。
2. main.xml中的代碼。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true" />
</LinearLayout>
3. strings.xml中的代碼。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, BasicViews6Activity!</string>
<string name="app_name">BasicViews6</string>
<string-array name="presidents_array">
<item>Dwight D. Eisenhower</item>
<item>John F. Kennedy</item>
<item>Lyndon B. Johnson</item>
<item>Richard Nixon</item>
<item>Gerald Ford</item>
<item>Jimmy Carter</item>
<item>Ronald Reagan</item>
<item>George H. W. Bush</item>
<item>Bill Clinton</item>
<item>George W. Bush</item>
<item>Barack Obama</item>
</string-array>
</resources>
4. BasicViews6Activity.java中的代碼。
public class BasicViews6Activity extends Activity {
String[] presidents;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
presidents =
getResources().getStringArray(R.array.presidents_array);
Spinner s1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, presidents);
s1.setAdapter(adapter);
s1.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> arg0,
View arg1, int arg2, long arg3)
{
int index = arg0.getSelectedItemPosition();
Toast.makeText(getBaseContext(),
"You have selected item : " + presidents[index],
Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) { }
});
}
}
Android瀑布流照片牆實現,體驗不規則排列的美感
傳統界面的布局方式總是行列分明、坐落有序的,這種布局已是司空見慣,在不知不覺中大家都已經對它 產生了審美疲勞。這個時候瀑布流布局的出現,就給人帶來了耳目一新的感覺,這種布
Android官方提供的支持不同屏幕大小的全部方法
本文將告訴你如何讓你的應用程序支持各種不同屏幕大小,主要通過以下幾種辦法:讓你的布局 能充分的自適應屏幕根據屏幕的配置來加載合適的UI布局確保正確的布局應用在正確的設備屏
Android ApiDemos示例解析(19) App->Alarm->Alarm Controller
Alarm Controller演示如何在Android應用中使用Alarm事件,其功能和java.util.Timer ,TimerTask類似。但Alarm可以即使
Android中R資源未找到至java文件報錯
R資源, 是本地xml資源的引用列表, 修改時, 有可能Gradle沒有生成, 相應的R資源;則會出現R資源未找到的錯; 導致Java文件異常, 報錯.可以使用: Syn