13

我試圖以編程方式向LinearLayout添加片段,但片段並未在整個佈局高度上伸展其內容。它的行爲像wrap_content而不是fill_parent。Android 3 - 添加片段到LinearLayout:fill_parent不起作用

另一方面,fill_parent在片段的寬度上工作。 我該如何改變這種行爲?

DashboardActivity:

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

    <fragment 
     android:id="@+id/fragment_dashboard" 
     android:layout_width="5dp" 
     android:layout_height="fill_parent" 
     android:layout_marginLeft="20dp" 
     android:layout_weight="1" 
     android:name="de.upb.cs.ginkgo.tablet.ui.fragments.DashboardFragment" > 
     <!-- Preview: [email protected]/fragment_dashboard --> 
     </fragment> 

    <LinearLayout 
     android:id="@+id/rightfrag" 
     android:layout_width="450dp" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:layout_marginTop="10dp" 
     android:layout_marginBottom="15dp" 
     android:layout_marginRight="10dp" > 
    </LinearLayout> 

</LinearLayout> 

其onCreate方法:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_dashboard); 
    //-- Instantiate Fragmentstuff 
    FragmentManager fragmentManager = getFragmentManager(); 
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
    ActivitystreamFragment asf = new ActivitystreamFragment(); 
    fragmentTransaction.add(R.id.rightfrag, asf); 
    fragmentTransaction.commit(); 
    } 

的ActivitystreamFragment XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1"> 

    <ListView 
     android:id="@+id/android:list" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:drawSelectorOnTop="true" 
     android:background="@drawable/back_rightfrag" 
     > 
</ListView> 
</LinearLayout> 

而其CreateView的:

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View root = inflater.inflate(R.layout.fragment_activitystream, null); 
     return root; 
     } 

回答

25

好的 - 這個很笨。我將我的容器從LinearLayout更改爲FrameLayout,並聲音:它正在工作。

+6

謝謝。但我不知道爲什麼FrameLayout可以工作,但LinearLayout不是 –

+0

您是否嘗試將FrameLayout *放入LinearLayout中? – zmbq

+6

@NguyenMinhBinh:由於FrameLayout將子項放在彼此的頂部,另一方面,LinearLayout將其子項放在下一個(水平或垂直)彼此之間。過渡確實奏效,但新添加的片段不在顯示中。 – Kuno