2016-01-27 94 views
0

我目前有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> 
+0

這個問題是無法回答的。您可以編寫一個包含0個片段的Android應用程序。或與數百。碎片是一種通用控制器重用和分組UI元素的便捷方式。使用它的正確時間是一個架構決定,取決於你如何編寫應用程序。 –

+0

我想創建片段,因爲我想讓我的應用與平板電腦尺寸兼容。例如,一方面我想用九個圖像按鈕的菜單頁面,一旦用戶點擊一個圖像按鈕,它會在另一個頁面的E.G頁面,菜單頁面和博物館頁面上顯示選定的活動。 –

回答

0

作爲每谷歌文檔,

甲片段表示行爲或在一個活動的用戶界面的一部分。通過將活動的佈局劃分爲片段,您可以在運行時修改活動的外觀,並將這些更改保留在活動管理的後端堆棧中。

如何劃分和何時劃分是作爲開發人員由您決定的。如果您認爲某項活動太重,無法處理,請將其分解爲模塊化和可重複使用的碎片。如果多個活動共享相同的UI部分,請將其作爲片段。

從給出的代碼我可以建議的是,actvitymain.XML,menufragment.XMLmuseumactivity. XML的佈局具有幾乎相同的用戶界面。所以你可以把它作爲一個片段,並將其包含在這3個XML文件中。

+0

我應該創建10個片段並離開10個活動。你怎麼看? –

+0

我不能給你一個完整的解決方案。隨你便。在編輯的答案中閱讀我的建議 – Msp

+0

它們不是一件容易的事。例如,讓菜單活動成爲一個片段,然後我將有菜單片段,然後你從菜單容器中加載其他活動菜單。 –