我在网上搜索过,仍无法找到有关如何从现有活动创建片段的任何帮助。例如,我有一个仪表板活动和一个菜单活动,我想创建片段,以便它将平板电脑上的两个页面显示为一个屏幕。
我也想从菜单页面中的片段创建到其他页面。例如,我希望一方面显示菜单页面,另一方面显示另一方面的类别。
我只能找到列表视图和详细示例。我是否需要为每个活动创建片段。请指教。
菜单活动。爪哇
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 :(得分:1)
每个活动都不需要单独的片段。你可以这样做: 使您的菜单活动成为片段,然后您将拥有菜单片段,然后您可以从片段容器中加载其他活动中的菜单。
同样,您可以在应用程序的任何位置将您的菜单和仪表板保存在一个活动中。
答案 1 :(得分:1)
您不需要为每个活动创建片段,您可以在问题中使用多个片段进行活动 AS,例如,如果您想在一侧显示菜单页面,并且在另一方面曾经点击其中一项活动: - 为此你可以创建两个片段并在你的单个活动中使用它们,如果你有这样的要求,你可以在许多活动中使用一个片段。
在Fragment上查看一些很好的教程
Android - Fragments和Official documentation
你会很容易地知道。