如何从现有活动的Android工作室片段 [英] How to create fragments in Android studios from existing activities
问题描述
我在网上搜查,仍然无法设法找到了如何从现有的活动片段任何帮助。比如我有一个仪表盘活动,我想用这样它都显示在平板电脑上的网页作为一个屏幕创建片段菜单的活动。
I have searched online and still cant manage to find any help of how to create fragments from existing activities. for example I have a dashboard activity and a menu activity which I would like to create fragments with so that it displays both of the pages on tablets as one screen.
我也想从菜单页面的其他页面片段创建为好。例如我想在一侧上显示菜单页并在另一侧的类别一旦上的活动之一点击
I also want to create from fragments from menu pages to other pages as well. for example I want to display menu page on one side and the categories on the other side once clicked on one of the activities.
我只能找到列表视图和细节的例子。我需要为每个活动创建片段。请指教。
I can only find list view and detail examples. Do I need to create fragments for each activity. Please advise.
菜单Actvity。 Java的
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
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
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>
我想创建这两个活动的片段,所以它适合平板电脑的屏幕尺寸。
I want to create a fragment from these two activities, so that it fits tablet screen size.
推荐答案
您不需要为每个活动单独片段。你可以这样说:
让您的菜单活性的片段这样,那么你将有菜单片段,那么你可以在你的其他活动从片段集装箱菜单。
You don't need separate fragments for each activity. You can do it like this: Make your menu activity a fragment so then you will have menu fragment then you can load menu in your other activities from a fragment container.
就像聪明人,你将能够保持你的菜单和仪表板于一体的应用内任何地方。
Like wise you will be able to hold your menu and dashboard in one activity any where in the app.
这篇关于如何从现有活动的Android工作室片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!