Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.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 创建包含3个片段的活动_Android_Android Layout_Android Activity_Android Studio - Fatal编程技术网

Android 创建包含3个片段的活动

Android 创建包含3个片段的活动,android,android-layout,android-activity,android-studio,Android,Android Layout,Android Activity,Android Studio,我目前有一个手机应用程序,它使用标签导航操作栏在三个片段之间导航 我现在想创建一个与此应用程序相当的平板电脑,但我不想使用actionbar,而是希望这三个片段彼此相邻。因此,每个片段将占据屏幕的1/3 问题是,我不知道该怎么做。我曾想过使用Android Studio的设计部分创建占位符,然后使用onCreate()方法通过膨胀占位符中的片段来填充占位符。但我仍然不知道如何处理这个问题 有人有什么想法吗?你可以制作3个占位符,每个占位符占据屏幕的三分之一,然后用片段填充。当然,你必须只在平板电

我目前有一个手机应用程序,它使用标签导航操作栏在三个片段之间导航

我现在想创建一个与此应用程序相当的平板电脑,但我不想使用actionbar,而是希望这三个片段彼此相邻。因此,每个片段将占据屏幕的1/3

问题是,我不知道该怎么做。我曾想过使用Android Studio的设计部分创建占位符,然后使用
onCreate()
方法通过膨胀占位符中的片段来填充占位符。但我仍然不知道如何处理这个问题


有人有什么想法吗?

你可以制作3个占位符,每个占位符占据屏幕的三分之一,然后用片段填充。当然,你必须只在平板电脑的布局上做这些

Fragment fragment1 = new FirstFragment();
Fragment fragment2 = new SecondFragment();
Fragment fragment3 = new ThirdFragment();

getSupportFragmentManager()
    .beginTransaction()
    .replace(R.id.placeholder1, fragment1)
    .replace(R.id.placeholder2, fragment2)
    .replace(R.id.placeholder3, fragment3)
    .commit();
编辑:布局示例

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <FrameLayout
        android:id="@+id/placeholder1"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"/>

    <FrameLayout
        android:id="@+id/placeholder2"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"/>

    <FrameLayout
        android:id="@+id/placeholder3"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"/>

</LinearLayout>

您可以制作3个占位符,每个占位符占据屏幕的三分之一,然后用片段填充它们。当然,你必须只在平板电脑的布局上做这些

Fragment fragment1 = new FirstFragment();
Fragment fragment2 = new SecondFragment();
Fragment fragment3 = new ThirdFragment();

getSupportFragmentManager()
    .beginTransaction()
    .replace(R.id.placeholder1, fragment1)
    .replace(R.id.placeholder2, fragment2)
    .replace(R.id.placeholder3, fragment3)
    .commit();
编辑:布局示例

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <FrameLayout
        android:id="@+id/placeholder1"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"/>

    <FrameLayout
        android:id="@+id/placeholder2"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"/>

    <FrameLayout
        android:id="@+id/placeholder3"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"/>

</LinearLayout>

您使用onCreate()的想法很好

基本上,您所需要的只是一个ViewGroup类型的容器,用于每个片段,然后使用FragmentTransactions相应地添加它们

考虑这一点:

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

    // Create fragments
    Fragment f0 = new Fragment0();
    Fragment f1 = new Fragment1();
    Fragment f2 = new Fragment2();

    // Add fragments
    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ft.add(R.id.container0, f0);
    ft.add(R.id.container1, f1);
    ft.add(R.id.container2, f2);
    ft.commit();
}
有关更多示例和背景信息,请参见此部分:

您使用onCreate()的想法很好

基本上,您所需要的只是一个ViewGroup类型的容器,用于每个片段,然后使用FragmentTransactions相应地添加它们

考虑这一点:

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

    // Create fragments
    Fragment f0 = new Fragment0();
    Fragment f1 = new Fragment1();
    Fragment f2 = new Fragment2();

    // Add fragments
    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ft.add(R.id.container0, f0);
    ft.add(R.id.container1, f1);
    ft.add(R.id.container2, f2);
    ft.commit();
}

有关更多示例和背景信息,请参见此:

是的,这也是我的想法。我目前有一个“闪屏”,决定设备是平板电脑还是手机,并启动相应的活动。你说你使用了占位符,但是那些占位符是什么?假设我使用
activity\u main\u table.xml
来做这个,哪些项目应该放在那里?哦。你跑得更快D
getSupportFragmentManager
afaik仅用于支持库碎片活动的兼容库上下文中,不是吗?我添加了带有占位符的布局示例@MABVT是的,如果您不使用支持库,只需使用getFragmentManager(),谢谢您的布局!那就是我要找的。你和@MABVT也谢谢你的代码,我马上就开始玩。是的,这也是我的想法。我目前有一个“闪屏”,决定设备是平板电脑还是手机,并启动相应的活动。你说你使用了占位符,但是那些占位符是什么?假设我使用
activity\u main\u table.xml
来做这个,哪些项目应该放在那里?哦。你跑得更快D
getSupportFragmentManager
afaik仅用于支持库碎片活动的兼容库上下文中,不是吗?我添加了带有占位符的布局示例@MABVT是的,如果您不使用支持库,只需使用getFragmentManager(),谢谢您的布局!那就是我要找的。你和@MABVT也谢谢你的代码,我马上就开始玩。