gpt4 book ai didi

android - 在大屏幕上动态显示 Activity 中的两个 fragment

转载 作者:搜寻专家 更新时间:2023-11-01 07:58:35 24 4
gpt4 key购买 nike

我刚刚开始学习 android,我有一个应用程序,它的主要 Activity 使用 fragment 管理器在启动时添加了一个 fragment 。然后程序在两个 fragment 之间切换以执行其功能,再次使用 fragment 管理器来替换 fragment 。

我希望此应用在大屏幕上同时显示两个 fragment 。我该怎么办

A)检测到有大屏幕

B) 如果是这种情况,主要 Activity 是否将两个 fragment 加载到单个 Activity 中?

每个 fragment 都有布局,主要 Activity 有一个布局,它只是一个空白的框架布局,在 onCreate 期间用 fragment 填充。请记住:有两个 fragment 和一个 Activity 。 Activity 使用 fragment 管理器将 fragment 加载到其容器中。

最佳答案

A) 要检测大屏幕,您可以使用此代码

public static boolean isTablet(Context context) {
return (context.getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK)
>= Configuration.SCREENLAYOUT_SIZE_LARGE;
}

如果设备在大屏幕上运行,它将返回 true。还要检查 this link

B) 通过代码添加 fragment ,您可以在同一 Activity 中拥有两个 fragment 。在主布局中有 2 个布局,它们将是两个 fragment 的容器。如果屏幕尺寸适合您,然后通过代码将 fragment 添加到每个 fragment 中。使用此代码。

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

<RelativeLayout
android:id="@+id/FragmentContainer1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RelativeLayout
android:id="@+id/FragmentContainer2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</LinearLayout>

然后添加Fragments。

fragmentTransaction.add(R.id.FragmentContainer1, fragment);
fragmentTransaction.add(R.id.FragmentContainer2, fragment);

检查 this link获取 Lars Vogel 的精彩教程。

关于android - 在大屏幕上动态显示 Activity 中的两个 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24570114/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com