【问题标题】:Aligning fragments with RelativeLayout使用 RelativeLayout 对齐片段
【发布时间】:2012-04-18 02:40:00
【问题描述】:

我在尝试将片段与RelativeLayout 对齐时遇到了真正的问题,尽管这看起来应该很简单。我只需要两个相邻的片段,如果我使用LinearLayout,它可以正常工作:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

   <fragment android:name="com.fragment.test.TitlesFragment"
            android:id="@+id/fragmentTitles"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
     />

    <FrameLayout 
            android:id="@+id/details" 
            android:layout_weight="1"
            android:layout_width="0px"
            android:layout_height="fill_parent" 
    />

</LinearLayout>

但是,如果我使用 RelativeLayout,则什么都不会显示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

   <fragment android:name="com.fragment.test.TitlesFragment"
            android:id="@+id/fragmentTitles"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
     />

    <FrameLayout 
            android:id="@+id/details" 
            android:layout_weight="1"
            android:layout_width="0px"
            android:layout_height="fill_parent" 
            android:layout_toRightOf="@id/fragmentTitles"            
    />

</RelativeLayout>

更新:

这是我看到的截图:

这是我正在使用的代码:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    >

    <LinearLayout
        android:orientation="horizontal"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:weightSum="3"
    >
        <fragment android:name="com.fragment1"
                android:id="@+id/fragment1"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_above="@id/statusUpdated"
         />

        <fragment android:name="com.fragment2"  
                android:id="@+id/fragment2"
                android:layout_width="0px"
                android:layout_weight="2"
                android:layout_height="fill_parent"
                android:layout_above="@id/statusUpdated"        
                android:layout_toRightOf="@id/fragment1"
         />

    </LinearLayout>

    <TextView android:id="@+id/statusUpdated" style="@style/Status" />

</RelativeLayout>

【问题讨论】:

    标签: android android-layout android-fragments android-relativelayout android-linearlayout


    【解决方案1】:

    您不能将权重与RelativeLayout 一起使用。基本上该属性会被忽略,这意味着您的两个片段都以0 的宽度呈现,因此它们不可见。

    我不确定您要完成什么,但您可能需要考虑将您的第一个示例(LinearLayout)包装成RelativeLayout - 换句话说:组合/集成两种布局。不过,这确实会导致您的视图层次结构中出现另一个级别。


    编辑:

    我还没有实际测试过它,但我认为你正在寻找这样的东西:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent">
    
        <LinearLayout 
            android:id="@+id/fragment_layout"
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent"
            android:layout_above="@+id/footer_view" 
            android:weightSum="3">
    
            <fragment 
                android:id="@+id/fragmentTitles" 
                android:name="com.fragment.test.TitlesFragment"
                android:layout_width="0dp" 
                android:layout_height="fill_parent"
                android:layout_weight="1" />
    
            <FrameLayout 
                android:id="@+id/details"
                android:layout_width="0dp" 
                android:layout_height="fill_parent"
                android:layout_weight="2" />
        </LinearLayout>
    
        <TextView 
            android:id="@+id/footer_view" 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:layout_alignParentBottom="true"
            android:background="#f00" 
            android:text="I am a footer" />
    
    </RelativeLayout>
    

    显然你可以用你喜欢的任何东西替换TextView

    【讨论】:

    • 我最终想要做的是在布局底部添加一个页脚,但我无法使用LinearLayout 让它工作。我能够使用RelativeLayout 显示页脚,但是片段不会出现,所以我只是想让片段出现(没有页脚),因此我的问题。听起来我将不得不弄清楚如何使用LinearLayout 让页脚工作。我基本上想要一个布局,其中左侧片段占据屏幕的 1/3,右侧占据屏幕的其余部分,并带有粘性页脚。
    • 对,这应该不难实现。请在我的答案中查看编辑并尝试一下。没试过,但应该(接近)你所追求的。
    • 我说得太早了。我实施了您的建议,并且在大多数情况下都有效,但是页脚似乎与列表中的最后一项重叠,因此最后一项被减半。呃,我在使用 Android 布局时真的很糟糕。
    • 你能提供一个屏幕截图吗?如果我使用如上所示的布局,给页脚一个透明的背景并为片段设置颜色,那么一切都很好地保持在页脚之上。如果其中一个片段与页脚重叠,我应该看到页脚后面的颜色(因为透明度)。 Screenshot with transparent footer.
    • 不客气——我们都有这样的夜晚。 :) 好在我喜欢摆弄布局。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-03
    • 1970-01-01
    • 2012-01-22
    • 2013-08-28
    • 1970-01-01
    相关资源
    最近更新 更多