【问题标题】:Fragments Layout in Two by Two Grid两两网格中的片段布局
【发布时间】:2012-12-13 20:23:00
【问题描述】:

我正在尝试取四个片段,并在屏幕的左上角、右上角、左下角和右下角以相等的比例放置它们。诚然,我迷路了。这是我对程序化布局的尝试:

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

【问题讨论】:

    标签: android android-layout dynamic fragment


    【解决方案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));
    

    【讨论】:

    • 这不仅有效,而且解决了片段布局的一个奇怪问题
    • 其中一个视图无缘无故地将各种小部件推到一起并重叠。当我尝试你的版本时,我正试图解决这个问题。
    【解决方案2】:

    经过多次尝试,这行得通:

        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 生气。

    【讨论】:

    • 这正是我要建议的。至于嵌套布局权重,您可以尝试使用 TableLayout 或 GridLayout 代替。或者使用一个RelativeLayout作为根,在最中心放置一个空的View对象,作为锚点来固定四个子fragment容器的大小。
    • 如果您想演示 relativelayout 版本,我很乐意将其标记为正确(假设它有效:P)。
    猜你喜欢
    • 2012-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-03
    • 2021-03-19
    • 1970-01-01
    • 2017-04-14
    相关资源
    最近更新 更多