来自 xml 的 ListFragment 布局 [英] ListFragment Layout from xml
问题描述
如何从 xml 创建ListFragment(支持 v4 库的)布局?ListFragment
以编程方式设置它的布局,所以如果你想修改它,你需要搞乱添加/删除视图.
How to create a ListFragment (of the support v4 library) layout from xml?
ListFragment
sets up its layout programmatically, so if you want to modify it, you need to mess with add/remove view.
推荐答案
我已将创建的布局转换为 xml,您可以添加/删除您的小部件或例如添加 pulltorefresh
解决方案.您不应更改所需的 ID.由于某些小部件需要内部 ID,因此需要调用辅助函数,并且它需要支持 v4 命名空间,因为常量是内部的,具有默认可见性.
I've converted the created layout to xml, you can add/remove your widgets or e.g. add pulltorefresh
solutions. You shouldn't change the needed ids.
Because of internal ids needed for some of the widgets an helper function needs to be called and it needs to be in support v4 namespace since constants are internal with default visibility.
list_loader.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/progress_container_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:visibility="gone" >
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<FrameLayout
android:id="@+id/list_container_id"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/empty_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" />
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="false" >
</ListView>
</FrameLayout>
</FrameLayout>
和android.support.v4.app
中的ListFragmentLayout
类(在你的项目中,你不需要修改support v4 jar)
and the class ListFragmentLayout
in android.support.v4.app
(in your project, you don't need to modify support v4 jar)
package android.support.v4.app;
import your.package.R;
import android.view.View;
public class ListFragmentLayout
{
public static void setupIds(View view)
{
view.findViewById(R.id.empty_id).setId(ListFragment.INTERNAL_EMPTY_ID);
view.findViewById(R.id.progress_container_id).setId(ListFragment.INTERNAL_PROGRESS_CONTAINER_ID);
view.findViewById(R.id.list_container_id).setId(ListFragment.INTERNAL_LIST_CONTAINER_ID);
}
}
在扩展 ListFragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.list_loader, container, false);
ListFragmentLayout.setupIds(view);
return view;
}
这篇关于来自 xml 的 ListFragment 布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!