2012-12-28 34 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