如何创建一个ListFragment(支持V4库)布局从XML? ListFragment
以编程方式设置其布局,因此如果要修改它,则需要混乱添加/删除视图。ListFragment布局从XML
回答
我已将创建的布局转换为xml,您可以添加/删除您的小部件或例如添加pulltorefresh
解决方案。你不应该改变需要的ID。 由于某些小部件需要内部id,因此需要调用帮助器函数,并且需要支持v4命名空间,因为常量是内部默认可见性。
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
(在你的项目,你并不需要修改支持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;
}
从androi d SDK(ListFragment)(http://developer.android.com/reference/android/app/ListFragment.html):
公共查看onCreateView(LayoutInflater吹气,ViewGroup中容器,包savedInstanceState)
提供默认实现返回一个简单的列表视图。子类可以覆盖以替换它们自己的布局。如果这样做,返回的视图层次结构必须有一个ListView,其ID是android.R.id.list,并且可以有一个兄弟视图ID android.R.id.empty,当列表为空时,该视图将被显示。
如果您用自己的自定义内容重写此方法,请考虑在布局文件中包含标准布局list_content,以便继续保留ListFragment的所有标准行为。特别是,这是目前唯一能够显示内置的不确定进度状态的方法。
所以,当你想让你自己的layout.xml在FragmentList上覆盖onCreateView并膨胀自己的布局。
例如:
创建一个Android的布局(yourlistlayout.xml),并把下面的XML上要显示你的ListFragment位置。
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
在
ListFragment类
把下面的代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.yourlistlayout, null);
return v;
}
由于id不同,这不适用于支持库 – sherpya 2014-04-10 23:03:38
- 1. 从XML布局
- 2. 不同布局的ListFragment
- 3. 从布局XML文件
- 4. 从布局改变.xml
- 5. Magento布局XML
- 6. xml文件vs xml布局
- 7. Android:从xml将布局加载到新布局
- 8. 布局XML不是从布局sw600dp文件夹
- 9. 从自定义布局创建xml布局。可能吗?
- 10. 用XML文件布局填充布局
- 11. 如何从另一个自定义XML布局引用自定义XML布局?
- 12. XML布局重叠
- 13. 包括XML布局
- 14. Android xml布局:toRightOf
- 15. Android和XML布局
- 16. 的Magento XML布局
- 17. 布局权重XML
- 18. XML布局问题
- 19. Android UI XML布局
- 20. 布局解析XML
- 21. 切换xml布局?
- 22. ScrollView在xml布局?
- 23. 的Android XML布局
- 24. 从XML和Java代码膨胀布局
- 25. 从服务器载入XML布局(android)
- 26. Magento:setParentBlock从一个布局xml文件中?
- 27. 如何调用从布局XML的Android
- 28. 从另一个XML加载布局
- 29. 修改从XML布局变量的Android
- 30. listview使用不同的布局从xml
请编辑此所以很明显的问题是什么(发布它作为一个问题),并把下面的解决方案中的答案部分。 – 2012-08-27 12:58:26
你有没有一个可以参考的例子? – sherpya 2012-08-27 15:30:58
该帖子应该只是一个问题和答案的形式。这看起来像一个解决方案,但它不清楚原来的问题是什么答案。 – 2012-08-27 15:56:21