如何使用片段在布局中的listview下面添加按钮。 我的代码如下:
片段java。
ListView listview;
Button button;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.category, container, false);
View footerView = inflater.inflate(R.layout.footer_category, null);
listview = (ListView) view.findViewById(R.id.categoryNewsItemGrid);
listview.addFooterView(footerView);
loadmore = (Button) footerView.findViewById(R.id.loadMore);
loadmore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Log.d("Mylog","Button can click");
}
});
return view;
}
/res/layout/category.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/categoryNewsItemGrid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#C5C5C5"
android:dividerHeight="2px" />
</LinearLayout>
/res/layout/footer_category.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/footerCategory"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/loadMore"
android:layout_width="fill_parent"
android:text="Load More" />
</LinearLayout>
我得到错误(Force Close),如果我在logcat中看到,我在第74行得到消息错误Android Runtime,第74行是:
View footerView = inflater.inflate(R.layout.footer_category, null);
我的问题是,我想在列表视图下方添加页脚按钮。像this screenshot一样。 感谢。
答案 0 :(得分:3)
您错过了layout_height
中的/res/layout/footer_category.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/footerCategory"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/loadMore"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Load More" />
</LinearLayout>