我想做一個簡單的支票簿應用程序,其MainActivity存儲事務列表。我希望屏幕頂部和底部的TextView分別顯示帳戶餘額和添加新交易的選項。我想要在該滾動之間的交易列表。我能夠實現一個ListView並添加一個頁眉和頁腳視圖,但是如果事務列表超出了屏幕的大小,頁眉和頁腳就可以滾動屏幕。如何在LinearLayout中實現ListView?
有沒有什麼辦法在線性佈局中定位ListView,或凍結頁眉/頁腳以保持屏幕?
這是到目前爲止我的XML文件:
<TextView
android:id="@+id/header_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/default_header_string">
</TextView>
<ListView
android:id="@+id/transaction_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
<TextView
android:id="@+id/footer_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/add_transaction_string">
</TextView>
這裏是我的onCreate,其中有沒有語法錯誤,但我無法點擊footerview增加一個交易:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkbook);
ListView list = (ListView) findViewById(R.id.transaction_list_view);
// Create a new Adapter
mAdapter = new TransactionAdapter(list.getContext());
// Inflate footerView and headerView
LayoutInflater inflater = getLayoutInflater();
TextView headerView = (TextView) inflater.inflate(R.layout.header_view, null);
TextView footerView = (TextView) inflater.inflate(R.layout.footer_view, null);
// Set listener for footerView
footerView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent transactionIntent = new Intent(CheckbookActivity.this, AddTransactionActivity.class);
startActivityForResult(transactionIntent, ADD_TRANSACTION_REQUEST);
}
});
list.setAdapter(mAdapter);
}
你能說我,你爲什麼取決於LinearLayout中。使用RelativeLayout可以輕鬆完成。 –
嗯,我試過只使用一個ListView,因爲你可以添加頁腳和頁眉視圖,但隨着列表長度的增加,這些視圖會從屏幕上滾動出來,我試圖找到一種方法將它們保留在屏幕上。我希望用戶總是看到帳戶餘額,並始終看到添加新交易的選項。 – AdamMc331
是的,我的朋友可以用RelativeLayout輕鬆實現LinearLayout。 –