簡體   English   中英

片段布局分為兩乘兩格

[英]Fragments Layout in Two by Two Grid

我正在嘗試截取四個片段,並將它們按相等的比例分布在屏幕的左上角,右上角,左下角和右下角。 我當然迷路了。 這是我對程序化布局的嘗試:

// 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個片段。

參考我之前的評論,這是如何使用“相對布局”和在中心設置的不可見錨視圖來避免嵌套布局權重的方法。

<?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));

經過更多嘗試,此方法有效:

    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感到憤怒。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM