用3个片段创建活动 [英] Creating activity with 3 fragments
问题描述
我目前有一个手机应用程序,该应用程序使用选项卡导航操作栏在三个片段之间导航.
I currently have an application for my phone which uses a tab navigation actionbar to navigate between three fragments.
我现在想创建一个与此应用程序相对应的平板电脑,但是我不想使用动作栏,而是希望三个片段彼此相邻.因此,每个片段将占满屏幕的1/3.
I now want to create a tablet equivalent of this app, but instead of using the actionbar, I want the three fragments living next to each other. So each fragment would fill up 1/3th of the screen.
问题是,我不知道该如何处理.我曾考虑过使用Android Studio的设计"部分创建占位符,然后使用onCreate()
方法通过填充其中的片段来填充这些占位符.但是我仍然不知道如何解决这个问题.
The problem is, I can't figure out how to approach this. I've thought of using the Design part of Android Studio to create placeholders, then use the onCreate()
method to fill those placeholders by inflating the fragments in it. But I still have no idea on how to approach this.
有人有什么想法吗?
推荐答案
您可以制作3个占位符,每个占位符占据屏幕的三分之一,然后用片段填充它们.当然,您只需要在平板电脑的布局中制作这些图片即可.
You can make 3 placeholders each taking one third of the screen and then fill them in with fragments. Of course you have to make those only in layout for tablets.
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个片段创建活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!