我正在使用自定义listView。 这是我的代码:
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
viewInit();
adapter = new myPlanAdapter(this);
LayoutInflater inflater = this.getLayoutInflater();
LinearLayout listFooterView = (LinearLayout) inflater.inflate(
R.layout.violet_footer, null);
listView.addFooterView(listFooterView);
LinearLayout listHeaderView = (LinearLayout) inflater.inflate(
R.layout.blue_header, null);
listView.addHeaderView(listHeaderView);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
这是我的xml
<?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:background="@drawable/bg"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="45dp"
android:background="@drawable/head"
android:orientation="horizontal"
android:weightSum="10" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_weight="9"
android:gravity="center"
android:text="My Exam"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/AddMyPlanButtonId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/add" />
</LinearLayout>
<ListView
android:cacheColorHint="@android:color/transparent"
android:id="@+id/myPlanListViewId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"/>
</LinearLayout>
这是footer.xml
<?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="25dp"
android:background="@drawable/trans_violet_half_round"
android:orientation="vertical" >
</LinearLayout>
我错过了什么?页眉和页脚未添加。
答案 0 :(得分:3)
@Ashish:请在LinearLayout中添加一些视图并尝试,就像我通过添加一个TextView更改了您的footer.xml文件一样。替换代码并检查结果。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/trans_violet_half_round"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test" />
</LinearLayout>
答案 1 :(得分:0)
试试这个
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/icicibg"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_weight="9"
android:gravity="center"
android:text="My Exam"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/AddMyPlanButtonId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/add" />
</RelativeLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
>
<ListView
android:cacheColorHint="@android:color/transparent"
android:id="@+id/myPlanListViewId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"/>
<RelativeLayoutandroid:layout_width="fill_parent"
android:layout_height="wrap_content">
android:layout_width="match_parent"
android:layout_height="25dp"
android:background="@drawable/trans_violet_half_round"
android:orientation="vertical"/ >
</LinearLayout>