2012-05-21 67 views
7

鑑於:的Android - 顯示/隱藏片段留下的空白區域

  1. 屏幕上的兩個垂直放置元件(ViewPager和片段)在所述第一當前所選片斷(ViewFlipper)
  2. 文本的動作之間切換在頂部片段中顯示基於Web的和基於WebView的視圖,並隱藏/顯示底部片段。

觀察:

  1. 隱藏底部片段離開其中底部片段位於一個空的空間。

我想無論是相對的LinearLayout(設置爲weight=1頂部片段),但都具有後底部片段被刪除我仍然有空白區域沒有影響底部

這裏的頂層設計文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<android.support.v4.view.ViewPager 
    android:id="@+id/pager" 
    android:layout_width="fill_parent" 
    android:layout_height="0dip" android:layout_weight="1"/> 

<!-- This gets replaced with appropriate fragment at run time --> 
<LinearLayout 
    android:id="@+id/scrollFragmentPlaceholder" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:minHeight="110dip" /> 
</LinearLayout> 

這裏的代碼,切換片段

Fragment scroll = getSupportFragmentManager().findFragmentById(R.id.scrollFragment); 
    if (scroll.isHidden() == isWebView) 
     return; // already handled, do nothing 
    FragmentTransaction tr = getSupportFragmentManager().beginTransaction(); 
    if (scroll != null && scroll.isAdded()) { 
     if (isWebView) { 
      tr.hide(scroll); 
     } else 
      tr.show(scroll); 
    } 
    tr.commit(); 

這裏它的外觀如下: Bottom fragment is hidden

+0

您是否嘗試過上仍然認爲做* .measure() ? – Codeman

+0

不是真的,但如果我顛倒了順序並顯示文本版本/隱藏底部,它顯示正常 – Bostone

+0

WebView for android是血腥的怪異。你能否包含一些截圖,以便我能更明確地看到你在說什麼?如果您希望它們佔用一半的可用空間,則還需要在兩個視圖上都有layoutWeight = 1。您還需要確保在父視圖中指定orientation =「vertical」。 – Codeman

回答

16

一個月後,我想出瞭如何去做。原來,只是隱藏片段是不夠的,基本上我需要隱藏/顯示包含片段的shell。它仍然是最初在XML中定義的LinearLayout。實際上,在該原始佈局上設置可見性以顯示/隱藏片段就足夠了。因此,從問題的代碼現在看起來像這樣:

public void onJobViewToggled(final boolean isWebView) { 
    if (isFinishing()) 
     return; 
    final Fragment scroll = getSupportFragmentManager().findFragmentById(R.id.scrollFragment); 
    if (scroll.isHidden() == isWebView) 
     return; // already handled, do nothing 
    final FragmentTransaction tr = getSupportFragmentManager().beginTransaction(); 
    if (scroll != null && scroll.isAdded()) { 
     if (isWebView) { 
      tr.hide(scroll); 
      // shell is the original placeholder 
      shell.setVisibility(View.GONE); 
     } else { 
      tr.show(scroll); 
      shell.setVisibility(View.VISIBLE); 
     } 
    } 
    tr.commit(); 
} 

請注意,您還是要顯示/隱藏此片段工作

+1

你是如何得到shell的? – Nick