简体   繁体   English

RelativeLayout-如果视图水平对齐,则无法使它们具有相同的垂直大小

[英]RelativeLayout - can't make views the same vertical size if they are horizontally aligned

I have a layout which has two CardView 's and looks like this: 我有一个具有两个CardView的布局,看起来像这样:

在此处输入图片说明

I wanted the right card to be fixed width (90dp) and the left one to automatically resize and fill the remaining space. 我希望右边的卡固定宽度(90dp),左边的卡自动调整大小并填充剩余空间。 I have currently done it like this: 我目前这样做是这样的:

    <RelativeLayout
        android:id="@+id/inventory_host_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true">

        <android.support.v7.widget.CardView
            android:id="@+id/inventory_last_scanned_wrapper_card"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="20dp"
            android:layout_toLeftOf="@+id/inventory_scanned_infobox_wrapper_card">

            ...

        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            android:id="@+id/inventory_scanned_infobox_wrapper_card"
            android:layout_width="90dp"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="false"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_centerVertical="false"
            android:layout_marginRight="20dp"
            android:layout_marginTop="20dp">

            ...

        </android.support.v7.widget.CardView>

    </RelativeLayout>

Left one can grow, so I also wanted the right card to always be the same height as the left one. 左张卡片可以增长,所以我也希望右张卡片的高度始终与左张卡片的高度相同。 Usually I do that kind of alignment by setting both layout_alignTop and layout_alignBottom properties to other View but those properties are not available this time, probably because that would create a circular reference (although I don't really understand how horizontal alignment could affect vertical). 通常,我通过将layout_alignToplayout_alignBottom属性都设置为其他View来进行这种对齐,但是这些属性这次不可用,可能是因为这会创建一个循环引用(尽管我不太了解水平对齐会如何影响垂直)。 What to do? 该怎么办? I have explored several other options but always get back to the same point except hardcoding heights or setting them programmatically which of course I want to avoid. 我探索了其他几个选项,但总是回到同一点,除了硬编码高度或通过编程设置它们之外,我当然要避免。

Where and why are you getting circular reference errors? 您在哪里以及为什么会收到循环参考错误? Just set 刚设定

android:layout_alignBottom="@id/inventory_last_scanned_wrapper_card"

on the card at the right side and everything should work. 在右侧的卡上,一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM