7

我在尝试将片段与 a 对齐时遇到了真正的问题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>

在此处输入图像描述

但是,如果我使用 a 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>
4

1 回答 1

6

您不能将权重与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你喜欢的任何东西替换。

于 2012-04-18T02:52:44.803 回答