相对布局,将一个视图与另一个视图底部对齐,但始终在另一个视图下面。

6
我正在处理一个布局问题,基于以下图片。 Layout problem 该图片代表一个RelativeLayout。我需要蓝色视图与黑色视图底部对齐。但是,如果黑色视图比红色视图和蓝色视图的总和短,我需要将蓝色视图放置在红色视图下方。我想要得到这个结果: Right result 我尝试使用以下xml:
<RelativeLayout
    android:id="@+id/container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <View
        android:id="@+id/blackView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:align_parentTop="true"
        android:align_parentLeft="true"/>
    <View
        android:id="@+id/redView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:align_parentTop="true"
        android:align_parentRight="true"
        android:layout_toRightOf="@+id/blackView"/>
    <View
        android:id="@+id/blueView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/blackView"
        android:layout_below="@+id/redView"
        android:layout_alignParentRight="true"/>
</RelativeLayout>

但是似乎layout_alignBottom比layout_below具有更高的优先级。 我还尝试将blueView与其父视图底部对齐,但结果是该父视图(高度为wrap_content)变得与其上级(整个屏幕高度)一样高。

有人遇到过同样的问题吗?


请发布完整的布局XML。 - Hamid Shatu
发布XML的有用部分 - Massimo
5个回答

8

请使用这个。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <View
        android:id="@+id/blackView"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:background="#000000" />

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.5" >

        <View
            android:id="@+id/redView"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_alignParentTop="true"
            android:background="#FF0000" />

        <View
            android:id="@+id/blueView"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_alignParentBottom="true"
            android:background="#003CFF" />
    </RelativeLayout>
</LinearLayout>

</LinearLayout>

2

问题已解决!

<LinearLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="100dp"
    android:orientation="horizontal" >

    <View
        android:id="@+id/blackView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <View
            android:id="@+id/redView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="bottom|right" >

            <View
                android:id="@+id/blueView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

技巧在于android:gravity="bottom|right"。感谢您的帮助!

1
尝试这个..

<LinearLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="200dp" >

        <View
            android:id="@+id/blackView"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.4"
            android:background="#000000" />

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.6" >

            <View
                android:id="@+id/redView"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_alignParentTop="true"
                android:background="#FF0000" />

            <View
                android:id="@+id/blueView"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_alignParentBottom="true"
                android:background="#003CFF" />
        </RelativeLayout>
    </LinearLayout>

问题在于容器(LinearLayout)有固定的高度。但是我需要在那里使用wrap_content,因为在该视图下面还有其他视图。 - Massimo
@Massimo 你可以在容器(LinearLayout)中使用 android:layout_height="0dp" 和 weight 属性,其中容器的权重为 0.4,其余视图的权重为 0.6。 - Hariharan

0

我可以通过使用一个零大小的视图来标记位置来解决它:

<RelativeLayout
android:id="@+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<View
    android:id="@+id/blackView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:align_parentTop="true"
    android:align_parentLeft="true"/>
<View 
    android:id="@+id/blackViewBotom"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_centerHorizontal="true"
    android:layout_below="@id/blackView"/>
<View
    android:id="@+id/redView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:align_parentTop="true"
    android:align_parentRight="true"
    android:layout_toRightOf="@+id/blackView"/>
<View
    android:id="@+id/blueView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/blackView"
    android:layout_below="@+id/redView"
    android:layout_above="@id/blackViewBotom"
    android:layout_alignParentRight="true"/>


-2

相对布局:
只需添加android:layout_alignParentBottom="true"
对于此底部视图,无需使用android:layout_below="@+id/txtTitle"属性。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接