2017-06-12 41 views
1

我試圖實現以下設計。 TextView應該按內容佔用空間,剩下的空間就是地圖。有兩個片段的佈局:一個片段和一個帶有Google Map的片段

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

我在哪裏犯了一個錯誤? 也許我錯了,在與幾個片段的應用程序的體系結構?

+0

爲什麼不只是這樣做:https://developer.android.com/guide/components/fragments.html#Adding? – pskink

+0

好吧,我想動態管理片段。 – tim4dev

+0

您正在使用兩個片段進行管理,而您可以通過使用單個片段來完成。 –

回答

1

你可以用這個

  1. 添加Android的嘗試:layout_height = 「0dp」
  2. 添加機器人: 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> 
+1

它工作50%/ 50% ,我這樣做: 機器人:weightSum = 「4」 機器人:layout_weight = 「3」 機器人:layout_weight = 「1」 – tim4dev

+1

不過,我不想碰的main.xml 我想讓它具有普遍性。但是,如果這是不可能的,那麼這個解決方案就可以工作。 – tim4dev

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" /> 

+0

在fragment_container_1中添加android:layout_weight =「1」 –

+0

它的工作原理,謝謝,但我認爲@「IntelliJ Amiya」的答案更完整。 – tim4dev

+0

好..但答案其實不是確切的答案..它總是會給你50-50佈局..在你的問題你提到,你需要textview的大小來包裝內容 –

-1

不給一個TextView大小和地圖的大小匹配父。如果沒有解決方案,請告訴我。