Android 包含两个片段的布局:一个片段和一个带有谷歌地图的片段

Android 包含两个片段的布局:一个片段和一个带有谷歌地图的片段,android,google-maps,android-layout,android-fragments,Android,Google Maps,Android Layout,Android Fragments,我正在尝试实现以下设计。 TextView应该按内容占据空间,其余的空间是地图 +----------------+ | | | Map | <-- id/fragment_container_1 | | | | +----------------+ | | | TextView | <-- id/fragment_containe

我正在尝试实现以下设计。 TextView应该按内容占据空间,其余的空间是地图

+----------------+
|                |
|    Map         | <-- id/fragment_container_1
|                |
|                |
+----------------+
|                |
|    TextView    | <-- id/fragment_container_2
|                |
+----------------+
然而,在那之后,我发现地图占据了所有可见的空间

+----------------+
|                |
|                |
|    Map         |
|                |
|                |
+----------------+
我哪里出错了?
也许我在应用程序的体系结构中有几个片段是错误的?

像这样编辑main.xml

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<FrameLayout
    android:layout_weight="1"
    android:id="@+id/fragment_container_1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<FrameLayout
    android:id="@+id/fragment_container_2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

你可以试试这个

  • 添加android:layout\u height=“0dp”
  • 添加android:weightSum
  • 最后

    <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:weightSum="2">
    
                <FrameLayout
                    android:layout_weight="1"
                    android:id="@+id/fragment_container_1"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                  />
    
                <FrameLayout
                    android:layout_weight="1"
                    android:id="@+id/fragment_container_2"
                    android:layout_width="match_parent"
                    android:layout_height="0dp" />
          </LinearLayout>
    
    
    
    不要给出
    textview
    大小和映射大小匹配父项。

    在片段容器中添加android:layout\u weight=“1”不起作用,谢谢,但我认为@IntelliJ Amiya“答案更完整。好的。”。。但这个答案实际上并不是确切的答案。它总是给你50-50个布局。。在你的问题中,youn提到你需要文本视图的大小来包装内容,为什么不这样做:?好吧,我想动态管理片段。你用两个片段来管理它,而你可以用一个片段来完成。它工作50%/50%,我这样做了:android:weightSum=“4”android:layout\u weight=“3”android:layout_weight=“1”但是,我不想接触main.xml,我想让它保持通用性。但是,如果这是不可能的,那么这个解决方案是有效的。
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    // map
    Fragment fragmentMap = fragmentManager.findFragmentByTag("ObserverMapFragment");
    if (fragmentMap == null)   {
        fragmentMap = ObserverMapFragment.newInstance();
        fragmentTransaction
                .replace(R.id.fragment_container_1, fragmentMap, "ObserverMapFragment");
    }
    // fargment 1
    Fragment fragmentObserver = fragmentManager.findFragmentByTag("ObserverFragment");
    if (fragmentObserver == null)   {
        fragmentObserver = ObserverFragment.newInstance();
        fragmentTransaction
                .replace(R.id.fragment_container_2, fragmentObserver, "ObserverFragment");
    }
    fragmentTransaction.commit();
    
    +----------------+
    |                |
    |                |
    |    Map         |
    |                |
    |                |
    +----------------+
    
    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    
    <FrameLayout
        android:layout_weight="1"
        android:id="@+id/fragment_container_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    
    <FrameLayout
        android:id="@+id/fragment_container_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    
    <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:weightSum="2">
    
                <FrameLayout
                    android:layout_weight="1"
                    android:id="@+id/fragment_container_1"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                  />
    
                <FrameLayout
                    android:layout_weight="1"
                    android:id="@+id/fragment_container_2"
                    android:layout_width="match_parent"
                    android:layout_height="0dp" />
          </LinearLayout>