[英]Android ListActivity - fixed header and footer
是否可以將ListActivity的頁眉和頁腳設置為固定在頂部和底部,因此只有內容(列表)滾動,而不是頁眉和頁腳?
我這兩個都設置如下:
View header = getLayoutInflater().inflate(R.layout.list_header, null);
View footer = getLayoutInflater().inflate(R.layout.list_footer, null);
ListView listView = getListView();
listView.addHeaderView(header);
listView.addFooterView(footer);
您可以使用自定義XML布局來實現它,您可以在其中設置頁眉,頁腳和列表的布局。
請注意,要與ListActivity
兼容,此布局必須包含一個ID為android.R.id.list
的ListView
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HEADER" />
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FOOTER" />
</LinearLayout>
並將其設置在ListActivity
如下所示:
public class TestActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
根據項目場景,這種方法比任何其他方法都要好,因為如果要自定義頁眉和頁腳,可以在適當的頁眉和頁腳布局中進行更改。 在你的布局xml文件中遵循以下內容:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<include
android:id="@+id/header_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="@layout/header.xml" />
<ListView
android:id="@+id/list_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:below="@id/header_layout"
android:above="@id/footer_layout" />
<include
android:id="@+id/footer_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="@layout/footer.xml" />
</RelativeLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/header_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your Application Title"/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/done_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Done"/>
</LinearLayout>
public class MainActivity extends ListActivity {
private ListView mListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mListView = (ListView) findViewById(R.id.list_view);
// Now you can do whatever you want
}
}
你可以這樣做,如下所示:
//your adapter for listview
mAdapter = new ToDoListAdapter(getApplicationContext());
// Put divider between ToDoItems and FooterView
getListView().setFooterDividersEnabled(true);
// TODO - Inflate footerView for footer_view.xml file
LayoutInflater layoutInflater = getLayoutInflater();
//text view or whatever you have for your footer
TextView footerView = null;
footerView=(TextView)layoutInflater.inflate(R.layout.footer_view,null);
// TODO - Add footerView to ListView
getListView().addFooterView(footerView);
getListView().setAdapter(mAdapter);
這是在你的onCreate()方法中。 它對我有用。 如果你發現任何錯誤,請發表評論
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.