如何在同一页面的 Fragment 中创建一个包含 3 个 Fragment 的 ViewPager?
How to create a ViewPager with 3 Fragments inside a Fragment on the same page?
我想创建一个应用程序来使用 ViewPager 在一个片段中显示 3 个不同的片段。片段将如下图所示:
Click here to see the picture
稍后,当用户单击其中一个片段时,将打开另一个片段以仅显示该特定片段。
这可能吗?任何建议将不胜感激!谢谢!
真的需要3分钟。
你必须给每个container/view/widget一个0dp
,意思是“match_constrains”,换句话说:你的大小将由你的约束决定(在它们被运行 来自 the algorithm).
在下面的布局中有三个小部件(我使用了“新”FragmentContainerView
,但它们也可以是 FrameLayout
个实例。
三者注意事项:
- width/height 设置为
0dp
(又名:match_constrains
)
片段一:
- 从上到下 Parent(屏幕顶部)
- 开始到 Parent 开始(left/start 屏幕)
- 片段二的结束(右)到开始(左)。
- 片段三的底部到顶部。
片段二:
- 从上到下 Parent(屏幕顶部)
- Parent
的开始(左)到结束(右)
- End(right) to End of Parent (right/end of the screen)
- 片段三的底部到顶部。
片段三:
- Fragment One 从上到下(这里我选择了 Fragment One,但本可以使用“2”,甚至将准则设置为 50%,这很好)
- Start 和 End 都指向各自的 parent(因为我们希望 FR3 使用所有可用宽度)
- Parent 的底部到底部(片段三一直向下)。
结果如下所示:
这是布局。
Now go and spend some time learning Constraint Layout. :)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragment_one"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@android:color/holo_blue_bright"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@+id/fragment_two"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/fragment_three" />
<androidx.fragment.app.FragmentContainerView
android:id="@id/fragment_two"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@android:color/holo_blue_dark"
app:layout_constraintStart_toEndOf="@id/fragment_one"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/fragment_three" />
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragment_three"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@android:color/holo_purple"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/fragment_one"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
我想创建一个应用程序来使用 ViewPager 在一个片段中显示 3 个不同的片段。片段将如下图所示:
Click here to see the picture
稍后,当用户单击其中一个片段时,将打开另一个片段以仅显示该特定片段。 这可能吗?任何建议将不胜感激!谢谢!
真的需要3分钟。
你必须给每个container/view/widget一个0dp
,意思是“match_constrains”,换句话说:你的大小将由你的约束决定(在它们被运行 来自 the algorithm).
在下面的布局中有三个小部件(我使用了“新”FragmentContainerView
,但它们也可以是 FrameLayout
个实例。
三者注意事项:
- width/height 设置为
0dp
(又名:match_constrains
)
片段一:
- 从上到下 Parent(屏幕顶部)
- 开始到 Parent 开始(left/start 屏幕)
- 片段二的结束(右)到开始(左)。
- 片段三的底部到顶部。
片段二:
- 从上到下 Parent(屏幕顶部)
- Parent 的开始(左)到结束(右)
- End(right) to End of Parent (right/end of the screen)
- 片段三的底部到顶部。
片段三:
- Fragment One 从上到下(这里我选择了 Fragment One,但本可以使用“2”,甚至将准则设置为 50%,这很好)
- Start 和 End 都指向各自的 parent(因为我们希望 FR3 使用所有可用宽度)
- Parent 的底部到底部(片段三一直向下)。
结果如下所示:
这是布局。
Now go and spend some time learning Constraint Layout. :)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragment_one"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@android:color/holo_blue_bright"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@+id/fragment_two"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/fragment_three" />
<androidx.fragment.app.FragmentContainerView
android:id="@id/fragment_two"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@android:color/holo_blue_dark"
app:layout_constraintStart_toEndOf="@id/fragment_one"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/fragment_three" />
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragment_three"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@android:color/holo_purple"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/fragment_one"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>