0

画面の左上、右上、左下、右下に4つのフラグメントを均等に配置しようとしています。私は確かに道に迷っています。プログラマティックレイアウトでの私の試みは次のとおりです。

// 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つのフラグメントに対して十分な柔軟性を備えたものが必要です。

4

2 に答える 2

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));
于 2012-12-28T17:16:10.073 に答える
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>

nester layout_weightがEclipseを怒らせるので、私はまだより良い答えを受け入れています。

于 2012-12-28T16:00:39.373 に答える