0

我已經在線搜索,仍然無法找到任何有關如何從現有活動創建片段的幫助。例如,我有一個儀表板活動和一個菜單活動,我想創建片段,以便將平板電腦上的兩個頁面顯示爲一個屏幕。如何在現有活動中在Android工作室中創建片段

我也想創建從菜單頁面到其他頁面的片段。例如,我想在一側顯示菜單頁面,一旦點擊其中一個活動,則顯示另一側的類別。

我只能找到列表視圖和詳細的例子。我是否需要爲每個活動創建片段?請指教。

Menu Actvity。 Java的

public class MenuActivity extends ActionBarActivity { 


    private Toolbar toolbar; 

    ImageButton museummenubtn; 
    ImageButton theatremenubtn; 
    ImageButton fooddrinkmenubtn; 
    ImageButton leisuremenubtn; 
    ImageButton shoppingmenubtn; 
    ImageButton historicalmenubtn; 
    ImageButton parkmenubtn; 
    ImageButton familyfunmenubtn; 
    ImageButton travelinformenubtn; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_menu); 

     ImageButton museummenubtn = (ImageButton) findViewById(R.id.museum_icon); 
     museummenubtn.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       Intent myIntent = new Intent(view.getContext(), MuseumActivity.class); 
       startActivityForResult(myIntent, 0); 
       finish(); 
      } 

     }); 

     ImageButton theatremenubtn = (ImageButton) findViewById(R.id.theatre_icon); 
     theatremenubtn.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       Intent myIntent = new Intent(view.getContext(), TheatreActivity.class); 
       startActivityForResult(myIntent, 0); 
       finish(); 
      } 

     }); 

     ImageButton fooddrinkmenubtn = (ImageButton) findViewById(R.id.fooddrink_icon); 
     fooddrinkmenubtn.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       Intent myIntent = new Intent(view.getContext(), FoodAndDrinksActivity.class); 
       startActivityForResult(myIntent, 0); 
       finish(); 
      } 

     }); 

     ImageButton leisuremenubtn = (ImageButton) findViewById(R.id.leisure_icon); 
     leisuremenubtn.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       Intent myIntent = new Intent(view.getContext(), LeisureActivity.class); 
       startActivityForResult(myIntent, 0); 
       finish(); 
      } 

     }); 

     ImageButton shoppingmenubtn = (ImageButton) findViewById(R.id.shopping_icon); 
     shoppingmenubtn.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       Intent myIntent = new Intent(view.getContext(), ShoppingActivity.class); 
       startActivityForResult(myIntent, 0); 
       finish(); 
      } 

     }); 

     ImageButton historicalmenubtn = (ImageButton) findViewById(R.id.histroy_icon); 
     historicalmenubtn.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       Intent myIntent = new Intent(view.getContext(), HistoricalActivity.class); 
       startActivityForResult(myIntent, 0); 
       finish(); 
      } 

     }); 

     ImageButton parkmenubtn = (ImageButton) findViewById(R.id.park_icon); 
     parkmenubtn.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       Intent myIntent = new Intent(view.getContext(), ParksActivity.class); 
       startActivityForResult(myIntent, 0); 
       finish(); 
      } 

     }); 

     ImageButton familyfunmenubtn = (ImageButton) findViewById(R.id.familyfun_icon); 
     familyfunmenubtn.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       Intent myIntent = new Intent(view.getContext(), FamilyFunActivity.class); 
       startActivityForResult(myIntent, 0); 
       finish(); 
      } 

     }); 

     ImageButton travelinformenubtn = (ImageButton) findViewById(R.id.travelinfor_icon); 
     travelinformenubtn.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       Intent myIntent = new Intent(view.getContext(), TravelInformationActivity.class); 
       startActivityForResult(myIntent, 0); 
       finish(); 
      } 

     }); 


     toolbar = (Toolbar) findViewById(R.id.tool_bar); 
     setSupportActionBar(toolbar); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 

     int id = item.getItemId(); 

     if (id == R.id.action_user) { 
      Intent intent= new Intent(this,DashboardActivity.class); 
      startActivity(intent); 
      return true; 
     } 

     int id1 = item.getItemId(); 

     if (id1 == R.id.action_setting) { 
      Intent intent= new Intent(this,DashboardActivity.class);// Settings Class once it is created. 
      startActivity(intent); 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

} 

activitymenu.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> 

MuseumActvity.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> 

<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/musehead" 
     android:scaleType="fitXY" 
     android:src="@drawable/museumhead" 
     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/imageButton" 
     android:scaleType="fitXY" 
     android:src="@drawable/gallerieshead" 
     android:background="#ffffff" /> 
</LinearLayout> 

</RelativeLayout> 

我想創建這兩個活動的片段,所以它適合平板電腦的屏幕尺寸。

+0

結構上(代碼明智),你只是膨脹一個視圖,而不是使用setContentView,並有onCreateView以及onCreate。除此之外,沒有其他更改。你到底有什麼問題? –

+0

我在創建片段時遇到問題,我是否需要爲每個活動創建一個片段?它是否正確?。我希望我的應用程序支持平板電腦以及手機。 –

+0

「我有問題x」不是很具描述性......是的,您需要爲每個* layout *創建一個片段,這些片段當前使用單個活動進行顯示。但是你還需要一個Activity類來顯示這些片段。如果您爲要嘗試轉換的活動添加代碼,可以給出更詳細的答案 –

回答

1

對於每項活動,您不需要單獨的片段。你可以這樣做: 讓你的菜單活動成爲一個片段,這樣你就會有菜單片段,然後你可以從片段容器中加載其他活動的菜單。

與智慧一樣,您將能夠在應用程序中的任何位置將菜單和儀表板保存在一個活動中。

+0

最大你有一個教程,我可以按照或任何幫助讚賞? –

+0

本教程將幫助您http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/ – MaxExplode

+0

感謝Max,感謝您的幫助。 –

1

您不需要爲每個活動創建片段,您可以在活動中使用多個片段 AS例如,如果您想在一側顯示菜單頁面,並且一旦單擊時顯示另一側的類別在其中一個活動: -爲此,您可以創建兩個片段,並在您的單個活動中同時使用這兩個片段,並且如果您有這樣的要求,則可以在許多活動中使用一個片段。

結帳片段上一些好的教程

Android - FragmentsOfficial documentation

你會很容易知道的。

+0

雖然這個鏈接可能回答這個問題,但最好在這裏包含答案的基本部分,並提供供參考的鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 - [發表評論](/ review/low-quality-posts/11031963) – ChrisStillwell

+1

我修改了我的答案@ChrisStillwell –

+0

Amit Vaghela我還在努力,請給我一個更像我的例子,因爲所有的片段都是關於listview和detail視圖。 Mine更像是所有圖像按鈕的menuview,然後是另一個頁面上的imageview。請指教? –

相關問題