2012-12-28 2 views
0

4 개의 조각을 화면의 왼쪽 상단, 오른쪽 상단, 왼쪽 하단 및 오른쪽 하단에 동일한 비율로 배치하려고합니다. 나는 틀림없이 잃어버린다.2x2 격자의 조각 레이아웃

// 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로 : 둥지 라의 layout_weight를 화나게 일식 만들면서

<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> 

나는 아직도 더 나은 대답 개방입니다.

+0

이 내가 제안하고 있었다 정확히 것입니다. 중첩 된 레이아웃 가중치의 경우 TableLayout 또는 GridLayout을 대신 사용해 볼 수 있습니다. 또는 RelativeLayout을 루트로 사용하고 빈 View 객체를 가운데에 놓고 앵커로 사용하여 네 개의 하위 조각 컨테이너의 크기를 수정합니다. – Karakuri

+0

relativelayout 버전을 시연하려면 올바른 것으로 표시해야합니다 (작동한다고 가정하면 : P). – anthropomo