在我的ListFragment中,我想要一个headerView使应用看起来像它的网站对应。
onCreateView():
View headerView = inflater.inflate(R.layout.header, null, false);
在onStart():
if(headerView != null) {
getListView().addHeaderView(headerView);
}
在暂停和恢复活动之后,headerView有一个很大的上边距正在出现故障:https://www.youtube.com/watch?v=Q_zhr8w5Yzg
header.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:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/news_bubble"
android:paddingTop="5dp"
android:paddingRight="10dp"
android:paddingBottom="20dp"
android:paddingLeft="10dp"
android:layout_marginTop="10dp"
android:text="@string/str_header"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@android:color/white" />
</LinearLayout>
编辑:这个问题已经解决了。如果有人偶然发现此错误,这是我的代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment, container, false);
listView = (ListView)rootView.findViewById(android.R.id.list);
headerView = inflater.inflate(R.layout.header, listView, false);
listView.addHeaderView(headerView);
[...]
return rootView;
}
答案 0 :(得分:1)
您需要将列表视图作为标题视图的容器。因此首先对listView进行充气,然后按如下方式对标题视图进行充气
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_layout,container,false);
ListView listView = (ListView)view.findViewById(R.id.my_list);
View headerView = inflater.inflate(R.layout.header, listView, false);
listView.addHeaderView(headerView);
return view;
}