Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android Studio中从现有活动创建片段_Android_Xml_Android Layout_Android Studio_Android Fragments - Fatal编程技术网

如何在Android Studio中从现有活动创建片段

如何在Android Studio中从现有活动创建片段,android,xml,android-layout,android-studio,android-fragments,Android,Xml,Android Layout,Android Studio,Android Fragments,我在网上搜索过,仍然找不到任何关于如何从现有活动中创建片段的帮助。例如,我有一个仪表板活动和一个菜单活动,我想用它创建片段,以便它将平板电脑上的两个页面显示为一个屏幕 我还想创建从菜单页面到其他页面的片段。例如,我希望在单击某个活动后,在一侧显示菜单页面,在另一侧显示类别 我只能找到列表视图和详细示例。我是否需要为每个活动创建片段。请告知 菜单活性。爪哇 public class MenuActivity extends ActionBarActivity { private Too

我在网上搜索过,仍然找不到任何关于如何从现有活动中创建片段的帮助。例如,我有一个仪表板活动和一个菜单活动,我想用它创建片段,以便它将平板电脑上的两个页面显示为一个屏幕

我还想创建从菜单页面到其他页面的片段。例如,我希望在单击某个活动后,在一侧显示菜单页面,在另一侧显示类别

我只能找到列表视图和详细示例。我是否需要为每个活动创建片段。请告知

菜单活性。爪哇

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>

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>

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


我想从这两个活动中创建一个片段,使其适合平板电脑屏幕大小。

您不需要为每个活动创建单独的片段。您可以这样做: 将您的菜单活动设置为片段,这样您将拥有菜单片段,然后您可以从片段容器中加载其他活动中的菜单


与wise一样,您可以在应用程序中的任何位置的一个活动中保存菜单和仪表板。

您不需要为每个活动设置单独的片段。您可以这样做: 将您的菜单活动设置为片段,这样您将拥有菜单片段,然后您可以从片段容器中加载其他活动中的菜单


像wise一样,您可以在应用程序中的任何位置的一个活动中保留菜单和仪表板。

您不需要为每个活动创建片段,您可以为活动使用多个片段如您的问题中所述,例如,如果您想在一侧显示菜单页面,在另一侧显示类别,单击其中一个活动时:-为此,您可以创建两个片段,并在单个活动中同时使用这两个片段,也可以使用一个片段在许多活动中,如果你有这样的要求

查看一些关于片段的好教程


您将很容易了解。

您不需要为每个活动创建片段,您可以为活动使用多个片段如您的问题中所述,例如,如果您想在一侧显示菜单页面,在另一侧显示类别,单击其中一个活动时:-为此,您可以创建两个片段,并在单个活动中同时使用这两个片段,也可以使用一个片段在许多活动中,如果你有这样的要求

查看一些关于片段的好教程


您将很容易了解。

在结构上(代码方面),您只需膨胀视图,而不是使用setContentView,并具有onCreateView和onCreate。除此之外,没有太多其他变化。你到底有什么问题?我在创建片段方面有问题,我是否需要为每个活动创建一个片段。这是正确的吗?。我希望我的应用程序支持平板电脑和手机。“我在x上有问题”不是很好的描述。。。是的,您需要为每个单独的布局创建一个片段,这些布局当前使用单个活动显示。但是您还需要一个活动类来显示这些片段。如果您为您试图转换的活动添加了代码,则可以提供更详细的答案。我已经用代码更新了我的问题。例如,我有一个菜单活动,然后我有各种不同的活动,如博物馆、剧院、食品和饮料等。我想创建片段,使其适合我的平板电脑大小,在一个视图上同时显示a和B。结构上(代码方面),您只需膨胀视图,而不是使用setContentView,并具有onCreateView和onCreate。除此之外,没有太多其他变化。你到底有什么问题?我在创建片段方面有问题,我是否需要为每个活动创建一个片段。这是正确的吗?。我希望我的应用程序支持平板电脑和手机。“我在x上有问题”不是很好的描述。。。是的,您需要为每个单独的布局创建一个片段,这些布局当前使用单个活动显示。但是您还需要一个活动类来显示这些片段。如果您为您试图转换的活动添加了代码,则可以提供更详细的答案。我已经用代码更新了我的问题。例如,我有一个菜单活动,然后我有各种不同的活动,如博物馆、剧院、食品和饮料等。我想创建碎片,使其适合我的平板电脑大小,在一个视图中同时显示a和B。Max你有我可以遵循的教程或任何帮助吗?本教程将帮助你感谢Max,感谢您的帮助。Max,您有我可以遵循的教程吗?或者有任何值得感谢的帮助吗?本教程将帮助您感谢Max,非常感谢您的帮助。Amit Vaghela,我仍在努力,请您给我一个更像我的示例,因为所有片段都是关于listview和detail view的。我的更像是带有所有图像按钮的menuview,然后是另一个页面上的imageview。请提供建议?请查看此示例-和@J.DoeAmit Vaghela,我非常感谢您的帮助,但是这些示例都是关于操作栏菜单的。地雷有点不同。我目前有10项活动。一个是菜单活动,其余的是类别活动,我目前已经创建了一个称为菜单片段的片段。我还需要创建多少片段?.Amit Vaghela我还在努力,你能给我一个更像我的例子吗,因为所有片段都是关于listview和detail view的。我的更像是带有所有图像按钮的menuview,然后是另一个页面上的imageview。请告知?chec