在android的ListView中添加标题
我需要在我的列表视图中添加标题,即使滚动列表,标题也应始终显示在android的ListView中添加标题,android,android-listview,Android,Android Listview,我需要在我的列表视图中添加标题,即使滚动列表,标题也应始终显示 在android列表视图中有可能吗?做一件事。将一个文本视图放在列表视图的正上方,并将文本设置为标题。这将满足您的需要。您可以在列表视图中添加标题,如下所示: View headerView = ((LayoutInflater)Activity.this.getSystemService(Activity.this.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.header, null, f
在android列表视图中有可能吗?做一件事。将一个文本视图放在列表视图的正上方,并将文本设置为标题。这将满足您的需要。您可以在列表视图中添加标题,如下所示:
View headerView = ((LayoutInflater)Activity.this.getSystemService(Activity.this.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.header, null, false);
list.addHeaderView(headerView)
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/header" />
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</TableLayout>
对我来说,有效的解决方案是创建一个TableLayout,它有一行标题和一行列表,如下所示:
View headerView = ((LayoutInflater)Activity.this.getSystemService(Activity.this.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.header, null, false);
list.addHeaderView(headerView)
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/header" />
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</TableLayout>
请确保ListView的id为@android:id/list。是的,以下是我所做的: 对于带有
ListActivity
或ListFragment
的自定义版面,版面必须包含ListView
,且android:id
属性设置为@android:id/list
1) main_layout.xml
:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
// custom Headers
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:gravity="center"
android:text="@string/systolic" />
<TextView
android:id="@+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:gravity="center"
android:text="@string/diastolic" />
<TextView
android:id="@+id/textView3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:gravity="center"
android:text="@string/date" />
</LinearLayout>
// This is important to be exactly like below ,android:id="@android:id/list"
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/linearLayout1" >
</ListView>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/bpSysHolder"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:layout_gravity="center_vertical"
android:text="TextView" >
</TextView>
<TextView
android:id="@+id/bpDiaHolder"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:layout_gravity="center_vertical"
android:text="TextView" >
</TextView>
<TextView
android:id="@+id/bpDtHolder"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:layout_gravity="center_vertical"
android:text="TextView" >
</TextView>
</LinearLayout>
3)
ListActivity
:
public class HistoryActivity extends ListActivity {
private SimpleCursorAdapter dbAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// this is neccessery for you
setContentView(R.layout.main_layout);
dao = new BpDAO(this);
Cursor bpList = dao.fetchAll_bp();
String[] from = new String[] { BpDAO.bp_SYS, BpDAO.bp_DIA, BpDAO.bp_DT };
int[] target = new int[] { R.id.bpSysHolder, R.id.bpDiaHolder,
R.id.bpDtHolder };
// And this for custom row layout for each list row
dbAdapter = new SimpleCursorAdapter(this, R.layout.custom_list_row,
bpList, from, target);
setListAdapter(dbAdapter);
}
@Override
protected void onDestroy() {
super.onDestroy();
dao.close();
}
@Override
public void onListItemClick(ListView l, View view, int position, long id) {
// TODO something on item click
}
}
如果需要,请选中:“请使用匹配父项而不是填充父项,因为它在材料设计中已被弃用