[英]How to add a title to a fragment in Android
在AndroidStudio中,我选择了“新建->片段->片段(列表)”来创建项目列表。 它可以正常工作并显示所有项目; 但是,我想在顶部有一个标题。
如果将另一个TextView添加到“ fragment_item_list.xml”文件中,则标题将出现在列表中的所有项目中。 从其他问题中我发现,如果我创建另一个LinearLayout xml布局文件(类似“ list_header.xml”),并在其中放置TextView,则可以使用布局充气器在片段活动(例如onCreateView
中将该布局充气。
View header = inflater.inflate(R.layout.list_header,container,false);
但是我不知道如何处理header
以使其显示在片段中的某个位置。 我可以通过调用container.addView(header);
将其添加到原始活动中container.addView(header);
但是如何将其添加到片段中?
我不想更改ActionBar标题,首先只是为了美观,其次因为该片段将其覆盖了。 理想情况下,将在警报对话框中出现标题。
让我试着了解你。 您想在片段列表中添加标题(例如标题),而不是在ActionBar中。 对?
查看fragment_item_list.xml,我们有以下内容:(我按照您的步骤操作:“新建->片段->片段(列表)”)
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:layoutManager="LinearLayoutManager"
tools:listitem="@layout/fragment_item" />
RecyclerView是用于填充列表的容器,换句话说,RecyclerView(或ListView)仅包含所有列表。 因此,您必须添加另一个容器(如LinearLayout,RelativeLayout等)作为布局的根,才能向该布局添加更多视图。 使用LinearLayout的示例:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
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="@+id/text_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World! I'm a header!"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/list"
android:name="com.veroapps.myapplication.ItemFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:layoutManager="LinearLayoutManager"
tools:listitem="@layout/fragment_item" />
</LinearLayout>
有了这个,您将在顶部有一个标题列表。 现在,您可以在ItemFragment(由Android Studio生成的片段的名称)中管理标题TextView。 当然,还有更多的方法可以做到这一点,但这是一个简单的方法。
希望这对您有所帮助。
在您的Fragment Java类中,应该有一个名为onCreateView
的方法,您可以在其中设置Fragment的布局(并在其中添加列表)。 它看起来像这样:
public static class ExampleFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.example_fragment, container, false);
}
}
在此Fragment中,此代码通过inflater.inflate(R.layout.example_fragment, container, false);
布局inflater.inflate(R.layout.example_fragment, container, false);
这意味着有一个xml文件,定义了名为example_fragment.xml
的布局。
找到此XML文件后,您的代码将相同。 您可以在其中添加其他视图。
您将要在该文件中已经在RecyclerView
或ListView
上方添加另一个TextView
。
像这样:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
...
android:text="YOUR TITLE" />
<RecyclerView
... />
</LinearLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.