【问题标题】:Activity Layout with Static and Dynamic Fragments?带有静态和动态片段的活动布局?
【发布时间】:2014-02-17 15:40:26
【问题描述】:

我希望创建这种类型的布局......

左侧用xml创建的一个静态片段

在底部的 xml 中创建的一个静态片段作为页脚

在顶部的 xml 中创建的一个静态片段作为标题

最后

基于从外部到活动的不同广播在运行时创建的不同动态片段的一个容器。

这是一个视觉效果......

如何在 xml 中构造这个活动的布局?

【问题讨论】:

    标签: android android-fragments fragment


    【解决方案1】:

    使用RelativeLayout 放置顶部和底部片段,然后在它们之间添加一个包含列表片段和最后一个动态片段的水平LinearLayout。片段的尺寸可以放在dimens文件中,我在xml中硬编码48dp;列表区域也占宽度的 20%,而内容区域占其余部分。下面是一些可以帮助您入门的内容:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <fragment
            android:id="@+id/top_fragment"
            android:name="com.adip.sampler.fragments.TopFragment"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:layout_alignParentTop="true" />
    
        <fragment
            android:id="@+id/bottom_fragment"
            android:name="com.adip.sampler.fragments.BottomFragment"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:layout_alignParentBottom="true" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/bottom_fragment"
            android:layout_below="@+id/top_fragment"
            android:baselineAligned="false"
            android:orientation="horizontal" >
    
            <fragment
                android:id="@+id/list_fragment"
                name="com.adip.sampler.fragments.ListFragment"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1" />
    
            <FrameLayout
                android:id="@+id/dynamic_area"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="4" >
            </FrameLayout>
        </LinearLayout>
    
    </RelativeLayout>
    

    或者,如果您只想拥有视图组而没有 fragment 标签,您可以使用此布局(您可以使用任何 ViewGroup 代替 FrameLayout,我出于效率原因使用它):

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <FrameLayout
            android:id="@+id/top_fragment"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:layout_alignParentTop="true" />
    
        <FrameLayout
            android:id="@+id/bottom_fragment"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:layout_alignParentBottom="true" />
    
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/bottom_fragment"
            android:layout_below="@+id/top_fragment"
            android:baselineAligned="false"
            android:orientation="horizontal" >
    
            <FrameLayout
                android:id="@+id/list_fragment"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1" />
    
            <FrameLayout
                android:id="@+id/dynamic_area"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="4" >
            </FrameLayout>
        </LinearLayout>
    
    </RelativeLayout>
    

    【讨论】:

    • 如果我想做同样的事情,但动态地在 xml 中没有片段...
    • 你能解释一下dynamically across the board no fragments in xml是什么意思吗?
    • 那么你可以在那里有一些视图组,而不是fragment 标签。要我编辑示例吗?
    • 已编辑,有意义吗?另外,如果您觉得某个答案有用,请记得点赞。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-24
    • 1970-01-01
    相关资源
    最近更新 更多