2013-02-07 19 views
0

我遇到了一個問題,其中我的LinearLayout內的片段視圖不伸展到父級的高度和寬度。但是因爲我在片段中有兩個片段,所以它有點複雜。片段的和子片段的LinearLayouts不伸展到整個父級大小

下面是活動的看法:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/main_content" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_gravity="center|fill" 
    android:orientation="vertical" 
    android:padding="8dp" > 

</LinearLayout> 

這裏的父片段XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/LinearLayout1" 
    android:background="#FF00FF" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:id="@+id/mtg_player1_fragment_wrapper" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:background="#0000FF" 
     android:orientation="horizontal" > 

    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/mtg_player2_fragment_wrapper" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:background="#FF0000" 
     android:orientation="horizontal" > 

    </LinearLayout> 

</LinearLayout> 

那孩子(播放器)片段XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mtg_player_life_wrapper" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <TextView 
     android:id="@+id/mtg_player_life_count" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="#00FF00" 
     android:gravity="center_vertical|center_horizontal" 
     android:text="0" 
     android:textSize="140sp" /> 

</LinearLayout> 

我加入了像這樣的母片段(通過點擊列表項):

getSupportFragmentManager().beginTransaction().replace(R.id.main_content, fragment, newFragmentClass.getSimpleName()).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN).commit(); 

而且孩子的片段像這樣(在父母的onViewCreated()):

mPlayer1Frag = (PlayerFragment) getSherlockActivity().getSupportFragmentManager().findFragmentById(R.id.player1_fragment_wrapper); 
if (mPlayer1Frag == null) { 
    mPlayer1Frag = new PlayerFragment(); 
} 
if (!mPlayer1Frag.isVisible()) { 
    ((RoboSherlockFragmentActivity)getSherlockActivity()).getSupportFragmentManager().beginTransaction().replace(R.id.player1_fragment_wrapper, mPlayer1Frag).commit(); 
} 

mPlayer2Frag = (PlayerFragment) getSherlockActivity().getSupportFragmentManager().findFragmentById(R.id.player2_fragment_wrapper); 
if (mPlayer2Frag == null) { 
    mPlayer2Frag = new PlayerFragment(); 
} 
if (!mPlayer2Frag.isVisible()) { 
    ((RoboSherlockFragmentActivity)getSherlockActivity()).getSupportFragmentManager().beginTransaction().replace(R.id.player2_fragment_wrapper, mPlayer2Frag).commit(); 
} 

我得到的是以下幾點:

Current (problematic) UI

什麼基本情況是:

  • 父代片段視圖didn垂直延伸以填充整個活動的視圖(不出現粉紅色);
  • TextView(綠色)未伸展以覆蓋子片段佈局(紅色/藍色);

我想要發生的事情是讓父UI在整個活動中伸展,讓孩子們覆蓋整個父母,讓TextView覆蓋整個孩子。我可以改變它使用一個片段(並可能做到這一點),但我寧願如果我能保持這種方式,因爲它感覺更有組織:)因爲它感覺更有組織:)

所以,問題是,我是什麼做錯了?這不能用另外兩個片段來完成嗎?

+0

如何在'onCreateView'方法中擴充這些片段的佈局? – Luksprog

+0

以最簡單的方式: 'return inflater.inflate(R.layout.parent_view,null);' –

+1

將其更改爲'return inflater.inflate(R.layout.parent_view,container,false);'(container is 「onCreateView」方法的第二個參數)並查看它是如何工作的。 – Luksprog

回答

2

FragmentonCreateView()方法,使用該方法的容器參數像這樣始終膨脹佈局文件:

inflater.inflate(R.layout.parent_view, container, false); 

這樣膨脹的視圖將被給予適當的LayoutParams並且將表現爲設計的。