我需要多少碎片?

时间:2016-01-27 21:26:52

标签: android xml android-layout android-fragments

我目前有10项活动。一个是菜单活动,其余是类别活动,我目前创建了一个名为menu fragment的片段。我需要创建多少片段?

我想从菜单页面中的片段创建到其他页面。例如,我希望一方面显示菜单页面,另一方面显示另一方面的类别。

我只能找到列表视图和详细示例。我是否需要为每个活动创建片段以及需要创建多少片段。我是否要创建一个新的大型布局xml.file请指教。

actvitymain.XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context=".MainActivity">

    <include
        android:id="@+id/tool_bar"
        layout="@layout/tool_bar"
        ></include>


    <RelativeLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="170dp"
        android:id="@+id/linearLayout"
        android:weightSum="1"
        android:background="#ffffff"
        android:layout_below="@+id/tool_bar">

        <ImageButton
            android:layout_width="125dp"
            android:layout_height="170dp"
            android:id="@+id/museum_icon"
            android:background="#ffffff"
            android:src="@drawable/museumicon"
            android:scaleType="fitXY" />

        <ImageButton
            android:layout_width="125dp"
            android:layout_height="170dp"
            android:id="@+id/theatre_icon"
            android:background="#00000000"
            android:src="@drawable/theatreicon"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/museum_icon"
            android:layout_toEndOf="@+id/museum_icon"
            android:scaleType="fitXY" />

        <ImageButton
            android:layout_width="125dp"
            android:layout_height="170dp"
            android:id="@+id/fooddrink_icon"
            android:src="@drawable/foodicon"
            android:background="#ffffff"
            android:layout_gravity="center_vertical"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/theatre_icon"
            android:layout_toEndOf="@+id/theatre_icon"
            android:scaleType="fitXY" />

    </RelativeLayout>

    <RelativeLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="170dp"
        android:layout_below="@+id/linearLayout"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/linearLayout2"
        android:background="#ffffff">

        <ImageButton
            android:layout_width="125dp"
            android:layout_height="170dp"
            android:id="@+id/leisure_icon"
            android:background="#ffffff"
            android:scaleType="fitXY"
            android:src="@drawable/leisureicon" />

        <ImageButton
            android:layout_width="125dp"
            android:layout_height="170dp"
            android:id="@+id/shopping_icon"
            android:background="#00000000"
            android:scaleType="fitXY"
            android:src="@drawable/shoppingicon"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/leisure_icon"
            android:layout_toEndOf="@+id/leisure_icon" />

        <ImageButton
            android:layout_width="125dp"
            android:layout_height="170dp"
            android:id="@+id/histroy_icon"
            android:src="@drawable/historicalicon"
            android:scaleType="fitXY"
            android:background="#ffffff"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/shopping_icon"
            android:layout_toEndOf="@+id/shopping_icon" />
    </RelativeLayout>
    <RelativeLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="170dp"
        android:id="@+id/linearLayout3"
        android:layout_below="@+id/linearLayout2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:background="#ffffff"
        android:layout_alignParentBottom="true">

        <ImageButton
            android:layout_width="125dp"
            android:layout_height="170dp"
            android:id="@+id/park_icon"
            android:background="#ffffff"
            android:scaleType="fitXY"
            android:src="@drawable/parkicon" />

        <ImageButton
            android:layout_width="125dp"
            android:layout_height="170dp"
            android:id="@+id/familyfun_icon"
            android:background="#00000000"
            android:scaleType="fitXY"
            android:src="@drawable/familyfunicon"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/park_icon"
            android:layout_toEndOf="@+id/park_icon" />

        <ImageButton
            android:layout_width="125dp"
            android:layout_height="170dp"
            android:id="@+id/travelinfor_icon"
            android:src="@drawable/travelicon"
            android:scaleType="fitXY"
            android:background="#ffffff"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/familyfun_icon"
            android:layout_toEndOf="@+id/familyfun_icon" />
    </RelativeLayout>


</RelativeLayout>

menufragment.XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context=".MenuFragment">

    <include
        android:id="@+id/tool_bar"
        layout="@layout/tool_bar"
        ></include>


    <RelativeLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="170dp"
        android:id="@+id/linearLayout"
        android:weightSum="1"
        android:background="#ffffff"
        android:layout_below="@+id/tool_bar">

        <ImageButton
            android:layout_width="125dp"
            android:layout_height="170dp"
            android:id="@+id/museum_icon"
            android:background="#ffffff"
            android:src="@drawable/museumicon"
            android:scaleType="fitXY" />

        <ImageButton
            android:layout_width="125dp"
            android:layout_height="170dp"
            android:id="@+id/theatre_icon"
            android:background="#00000000"
            android:src="@drawable/theatreicon"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/museum_icon"
            android:layout_toEndOf="@+id/museum_icon"
            android:scaleType="fitXY" />

        <ImageButton
            android:layout_width="125dp"
            android:layout_height="170dp"
            android:id="@+id/fooddrink_icon"
            android:src="@drawable/foodicon"
            android:background="#ffffff"
            android:layout_gravity="center_vertical"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/theatre_icon"
            android:layout_toEndOf="@+id/theatre_icon"
            android:scaleType="fitXY" />

    </RelativeLayout>

    <RelativeLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="170dp"
        android:layout_below="@+id/linearLayout"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/linearLayout2"
        android:background="#ffffff">

        <ImageButton
            android:layout_width="125dp"
            android:layout_height="170dp"
            android:id="@+id/leisure_icon"
            android:background="#ffffff"
            android:scaleType="fitXY"
            android:src="@drawable/leisureicon" />

        <ImageButton
            android:layout_width="125dp"
            android:layout_height="170dp"
            android:id="@+id/shopping_icon"
            android:background="#00000000"
            android:scaleType="fitXY"
            android:src="@drawable/shoppingicon"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/leisure_icon"
            android:layout_toEndOf="@+id/leisure_icon" />

        <ImageButton
            android:layout_width="125dp"
            android:layout_height="170dp"
            android:id="@+id/histroy_icon"
            android:src="@drawable/historicalicon"
            android:scaleType="fitXY"
            android:background="#ffffff"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/shopping_icon"
            android:layout_toEndOf="@+id/shopping_icon" />
    </RelativeLayout>
    <RelativeLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="170dp"
        android:id="@+id/linearLayout3"
        android:layout_below="@+id/linearLayout2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:background="#ffffff"
        android:layout_alignParentBottom="true">

        <ImageButton
            android:layout_width="125dp"
            android:layout_height="170dp"
            android:id="@+id/park_icon"
            android:background="#ffffff"
            android:scaleType="fitXY"
            android:src="@drawable/parkicon" />

        <ImageButton
            android:layout_width="125dp"
            android:layout_height="170dp"
            android:id="@+id/familyfun_icon"
            android:background="#00000000"
            android:scaleType="fitXY"
            android:src="@drawable/familyfunicon"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/park_icon"
            android:layout_toEndOf="@+id/park_icon" />

        <ImageButton
            android:layout_width="125dp"
            android:layout_height="170dp"
            android:id="@+id/travelinfor_icon"
            android:src="@drawable/travelicon"
            android:scaleType="fitXY"
            android:background="#ffffff"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/familyfun_icon"
            android:layout_toEndOf="@+id/familyfun_icon" />
    </RelativeLayout>


</RelativeLayout>

museumactivity。 XML

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        tools:context=".MainActivity">

        <include
            android:id="@+id/tool_bar"
            layout="@layout/tool_bar"
            ></include>


        <RelativeLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="170dp"
            android:id="@+id/linearLayout"
            android:weightSum="1"
            android:background="#ffffff"
            android:layout_below="@+id/tool_bar">

            <ImageButton
                android:layout_width="125dp"
                android:layout_height="170dp"
                android:id="@+id/museum_icon"
                android:background="#ffffff"
                android:src="@drawable/museumicon"
                android:scaleType="fitXY" />

            <ImageButton
                android:layout_width="125dp"
                android:layout_height="170dp"
                android:id="@+id/theatre_icon"
                android:background="#00000000"
                android:src="@drawable/theatreicon"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/museum_icon"
                android:layout_toEndOf="@+id/museum_icon"
                android:scaleType="fitXY" />

            <ImageButton
                android:layout_width="125dp"
                android:layout_height="170dp"
                android:id="@+id/fooddrink_icon"
                android:src="@drawable/foodicon"
                android:background="#ffffff"
                android:layout_gravity="center_vertical"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/theatre_icon"
                android:layout_toEndOf="@+id/theatre_icon"
                android:scaleType="fitXY" />

        </RelativeLayout>

        <RelativeLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="170dp"
            android:layout_below="@+id/linearLayout"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:id="@+id/linearLayout2"
            android:background="#ffffff">

            <ImageButton
                android:layout_width="125dp"
                android:layout_height="170dp"
                android:id="@+id/leisure_icon"
                android:background="#ffffff"
                android:scaleType="fitXY"
                android:src="@drawable/leisureicon" />

            <ImageButton
                android:layout_width="125dp"
                android:layout_height="170dp"
                android:id="@+id/shopping_icon"
                android:background="#00000000"
                android:scaleType="fitXY"
                android:src="@drawable/shoppingicon"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/leisure_icon"
                android:layout_toEndOf="@+id/leisure_icon" />

            <ImageButton
                android:layout_width="125dp"
                android:layout_height="170dp"
                android:id="@+id/histroy_icon"
                android:src="@drawable/historicalicon"
                android:scaleType="fitXY"
                android:background="#ffffff"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/shopping_icon"
                android:layout_toEndOf="@+id/shopping_icon" />
        </RelativeLayout>
        <RelativeLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="170dp"
            android:id="@+id/linearLayout3"
            android:layout_below="@+id/linearLayout2"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:background="#ffffff"
            android:layout_alignParentBottom="true">

            <ImageButton
                android:layout_width="125dp"
                android:layout_height="170dp"
                android:id="@+id/park_icon"
                android:background="#ffffff"
                android:scaleType="fitXY"
                android:src="@drawable/parkicon" />

            <ImageButton
                android:layout_width="125dp"
                android:layout_height="170dp"
                android:id="@+id/familyfun_icon"
                android:background="#00000000"
                android:scaleType="fitXY"
                android:src="@drawable/familyfunicon"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/park_icon"
                android:layout_toEndOf="@+id/park_icon" />

            <ImageButton
                android:layout_width="125dp"
                android:layout_height="170dp"
                android:id="@+id/travelinfor_icon"
                android:src="@drawable/travelicon"
                android:scaleType="fitXY"
                android:background="#ffffff"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/familyfun_icon"
                android:layout_toEndOf="@+id/familyfun_icon" />
        </RelativeLayout>


    </RelativeLayout>

theatreactivity.XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context=".MainActivity">

    <include
        android:id="@+id/tool_bar"
        layout="@layout/tool_bar"
        ></include>

    <fragment
        android:id="@+id/Menufragment"
        android:name="com.bradvisor.bradvisor.MenuFragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        tools:layout="@layout/fragment_menu" />


    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="260dp"
        android:layout_below="@+id/tool_bar"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:weightSum="1"
        android:id="@+id/linearLayout4">

        <ImageButton
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/logo"
            android:scaleType="fitCenter"
            android:src="@drawable/logofinal"
            android:background="#ffffff" />
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="260dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/linearLayout4" >

        <ImageButton
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/theatrehead"
            android:scaleType="fitXY"
            android:src="@drawable/theatrehead"
            android:background="#ffffff" />
    </LinearLayout>

</RelativeLayout> 

1 个答案:

答案 0 :(得分:0)

根据Google文档,

  

Fragment表示Activity中的行为或用户界面的一部分。通过将活动的布局划分为片段,您可以在运行时修改活动的外观,并将这些更改保留在由活动管理的后台堆栈中。

作为开发者,如何划分以及何时划分由您决定。如果您认为活动太重而无法处理,请将其分解为模块化和可重用的碎片。如果多个活动共享相同的UI部分,请将其拉​​为片段。

根据我建议的代码,actvitymain.XMLmenufragment.XMLmuseumactivity. XML的布局几乎具有相同的UI。因此,您可以将其作为片段拉出来并将其包含在这3个XML文件中。