2012-12-28 35 views
0

我試圖將四個片段按等比例排列在屏幕的左上角,右上角,左下角和右下角。我承認輸了。這是我嘗試在一個綱領性的佈局:由兩個網格組成的兩個片段佈局

// instantiated fragments this way for viewpager in phone version: 
    fragments = new Vector<Fragment>(); 
    fragments.add(Fragment.instantiate(this, LevelsFragment.class.getName())); 
    fragments.add(Fragment.instantiate(this, KBFragment.class.getName())); 
    fragments.add(Fragment.instantiate(this, WaveFragment.class.getName())); 
    fragments.add(Fragment.instantiate(this, EnvFragment.class.getName())); 

    FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
    ft.add(R.id.linear_layout, fragments.get(2)); 
    ft.add(R.id.linear_layout, fragments.get(3)); 
    ft.add(R.id.linear_layout, fragments.get(1)); 
    ft.add(R.id.linear_layout, fragments.get(0));  
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 
    ft.commit(); 
    getSupportFragmentManager().executePendingTransactions(); 

而且基本的XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/linear_layout" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

</LinearLayout> 

這只是填滿了第一個片段的畫面。我更喜歡程序化的解決方案,以便可以在以後更換碎片。

編輯:我應該提一下,我希望有足夠靈活的東西可以在上面2個片段和下面1個片段。

回答

1

參考我以前的評論,以下是如何使用相對佈局以及在中心設置的不可見錨點視圖以避免嵌套佈局權重。

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

    <View 
     android:id="@+id/anchor" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_centerInParent="true" /> 

    <FrameLayout 
     android:id="@+id/top_left" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_above="@+id/anchor" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_toLeftOf="@+id/anchor" /> 

    <FrameLayout 
     android:id="@+id/top_right" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_above="@+id/anchor" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:layout_toRightOf="@+id/anchor" /> 

    <FrameLayout 
     android:id="@+id/bottom_left" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/anchor" 
     android:layout_toLeftOf="@+id/anchor" /> 

    <FrameLayout 
     android:id="@+id/bottom_right" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:layout_below="@+id/anchor" 
     android:layout_toRightOf="@+id/anchor" /> 

</RelativeLayout> 

添加片段作爲前:

ft.add(R.id.top_left, fragments.get(2)); 
ft.add(R.id.top_right, fragments.get(3)); 
ft.add(R.id.bottom_left, fragments.get(1)); 
ft.add(R.id.bottom_right, fragments.get(0)); 
+0

這不僅適用,而且可以清除碎片佈局中的一個奇怪問題 – anthropomo

+0

出於好奇,它解決了什麼問題? – Karakuri

+0

其中一個觀點是推擠在一起,重疊各種小工具,沒有明顯的原因。當我嘗試你的版本時,我試圖對此進行整理。 – anthropomo

0

更多的嘗試後,這個工程:

ft.add(R.id.top_left, fragments.get(2)); 
    ft.add(R.id.top_right, fragments.get(3)); 
    ft.add(R.id.bottom_left, fragments.get(1)); 
    ft.add(R.id.bottom_right, fragments.get(0)); 

與該XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/linear_layout" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:orientation="vertical"> 
<LinearLayout 
    android:baselineAligned="false" 
    android:layout_weight="1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 
    <RelativeLayout 
     android:layout_weight="1" 
     android:id="@+id/top_left" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

    </RelativeLayout> 
    <RelativeLayout 
     android:layout_weight="1" 
     android:id="@+id/top_right" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

    </RelativeLayout> 
</LinearLayout> 
<LinearLayout 
    android:baselineAligned="false" 
    android:layout_weight="1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 
    <RelativeLayout 
     android:layout_weight="1"  
     android:id="@+id/bottom_left" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

    </RelativeLayout> 
    <RelativeLayout 
     android:layout_weight="1" 
     android:id="@+id/bottom_right" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

    </RelativeLayout> 
</LinearLayout> 
</LinearLayout> 

我還是開到一個更好的答案爲巢layout_weight使Eclipse生氣。

+0

這正是我正要建議。至於嵌套佈局權重,您可以嘗試使用TableLayout或GridLayout。或者使用RelativeLayout作爲根,將一個空的View對象放在正中,並將其用作錨點來修復四個子碎片容器的大小。 – Karakuri

+0

如果你想演示相關佈局版本,我很樂意將其標記爲正確的(假設它工作:P)。 – anthropomo