【发布时间】:2014-05-31 19:07:35
【问题描述】:
我正在尝试修改现有代码,同时是 Android 新手。如果我所解释的任何概念或假设在这个世界上没有意义,请告诉我!
我的目标是在 XML 中为我的列表视图的空视图定义一个布局。今天,我们通过编程调整 onActivityCreated 中的默认空视图来实现此功能,而我的 CustomListFragment 源自 ListFragment:
setEmptyText(getResources().getString(R.string.listview_empty));
TextView emptyTextView = (TextView) getListView().getEmptyView();
emptyTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.listview_empty_text_size));
emptyTextView.setTextColor(getResources().getColor(R.color.listview_empty_text));
我宁愿在 XML 中定义我的空视图的样式。它更干净,我可以使用自定义布局,而现在它只允许单个字符串。
它似乎依赖于默认的列表视图布局(因为我在片段代码中找不到任何inflate),其中包含一个空文本视图、一个进度指示器等。我试图提供我的自定义布局;这是我的 XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No items to show."/>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</LinearLayout>
现在我在CustomListFragment 中覆盖onCreateView:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.custom_listview, container, false);
return view;
}
我从onActivityCreated 中删除了我在上面发布的那段代码。
现在,我遇到了崩溃,因为ListFragment 的mProgressContainer 为空。它最初是由ensureList()设置的:
mProgressContainer = root.findViewById(INTERNAL_PROGRESS_CONTAINER_ID);
我应该在我的自定义布局中定义一个“进度容器”控件吗?在我看来这不是正确的方法,因为我需要将id 设置为INTERNAL_PROGRESS_CONTAINER_ID。
为空文本情况提供自定义 XML 定义视图的推荐方式是什么?
【问题讨论】: