【问题标题】:Layout with two fragments: one fragment and a fragment with the Google Map带有两个片段的布局:一个片段和一个带有 Google 地图的片段
【发布时间】:2017-06-12 11:16:40
【问题描述】:

我正在尝试实现以下设计。 TextView 应该按内容占用空间,剩下的空间是一张地图。

+----------------+
|                |
|    Map         | <-- id/fragment_container_1
|                |
|                |
+----------------+
|                |
|    TextView    | <-- id/fragment_container_2
|                |
+----------------+

main.xml

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

    <FrameLayout
        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>

fragment_1.xml

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Client Label" />
</LinearLayout>

map.xml

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

    <com.google.android.gms.maps.MapView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/map_view_observer" />
</LinearLayout>

Java 代码:

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         |
|                |
|                |
+----------------+

我在哪里做错了? 也许我在具有多个片段的应用程序架构上错了?

【问题讨论】:

标签: android google-maps android-layout android-fragments


【解决方案1】:

你可以试试这个

  1. 添加 android:layout_height="0dp"
  2. 添加 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>

【讨论】:

  • 它工作 50% / 50%,我这样做了:android:weightSum="4" android:layout_weight="3" android:layout_weight="1"
  • 但是,我不想触及 main.xml 我想让它通用。但是,如果这是不可能的,那么这个解决方案就可以了。
【解决方案2】:

像这样编辑你的 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" />

【讨论】:

  • 在fragment_container_1中添加android:layout_weight="1"
  • 它有效,谢谢,但我认为@“IntelliJ Amiya”的答案更完整。
  • 好的..但这个答案实际上并不是确切的答案..它总是会给你50-50个布局..在你提到你需要textview的大小来包装内容的问题中
【解决方案3】:

不要给 textview 大小和地图大小匹配的父级。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-13
    • 1970-01-01
    • 2012-10-25
    • 1970-01-01
    • 2017-05-10
    相关资源
    最近更新 更多