我正在通過教程http://www.vogella.de/articles/AndroidListView/article.html 瞭解如何使用ListActivity。我試圖讓列表滾動,但意識到你不能在佈局文件中使用scrollview來實現這一點。谷歌搜索後,我意識到Android不允許這樣做,但我一直無法找到一個似乎解釋如何使滾動發生的答案。Android ListActivity Scrollview如何獲取滾動列表
的代碼如下:
public class MyListActivityActivity extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" , "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" };
/*LayoutInflater factory = getLayoutInflater();
LinearLayout footer =
(LinearLayout) factory.inflate(R.layout.main, null);
getListView().addFooterView(footer); */
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.rowlayout, R.id.label, values);
setListAdapter(adapter);
}
protected void onListItemClick(ListView l, View v, int position, long id) {
String item = (String) getListAdapter().getItem(position);
Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();
}
}
//的XML文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/icon"
android:layout_width="22px"
android:layout_height="22px"
android:layout_marginLeft="4px"
android:layout_marginRight="10px"
android:layout_marginTop="4px"
android:src="@drawable/ic_launcher" >
</ImageView>
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label"
android:textSize="20px" >
</TextView>
</LinearLayout>
目前,更多的價值,你把數組,較小的列表成爲以確保它適合在屏幕上。我希望文本保持相同的大小,並讓它成爲一個可以滾動的長列表。
我讀過關於addHeader和addFooter的地方,它可以使滾動發生。但任何人都可以向我解釋或引導我有一個關於如何實現這一點的更多信息的來源?
我會很感激任何輸入。謝謝
你使用的是什麼版本的android?在4.0.3上它會自動爲我滾動。 – mpeerman 2012-03-17 11:09:56
你的問題中的XML沒有包含「ListView」 - 你的意思是向我們展示不同的佈局? – dldnh 2012-03-17 11:16:57
謝謝大家。我想我什麼也沒有嚇到。 – railslearner 2012-03-18 02:50:03