簡體   English   中英

如何在Android中為片段添加標題

[英]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文件后,您的代碼將相同。 您可以在其中添加其他視圖。

您將要在該文件中已經在RecyclerViewListView上方添加另一個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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM