android相对布局低于2项

Ben*_*Ben 53 android android-relativelayout

我有一个像这样的相对布局:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/single_row"
    android:padding="12dip">

    <ImageView
        android:id="@+id/page_image"
        android:layout_marginRight="6dip"
        android:layout_width="66dip"
        android:layout_height="66dip"
        android:layout_alignParentLeft="true"
        android:src="@drawable/no_photo" />
    <TextView
        android:id="@+id/page_name"
        style="@style/pulse_content"
        android:layout_alignTop="@id/page_image"
        android:layout_toRightOf="@id/page_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/page_desc"
        android:layout_below="@id/page_name"
        style="@style/pulse_content"
        android:layout_alignLeft="@id/page_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Principal Consultant" />
    <Button
        android:id="@+id/follow_button"
        android:layout_below="@id/author_image"
        android:layout_marginTop="15dip"
        android:layout_alignParentBottom="true"
        android:text="Follow"
        style="@style/follow_button" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是我希望follow_button低于page_desc和page_image.有时,page_desc内容的高度会大于图像的大小,有时则不会.问题是,如果我将follow_button设置在图像或描述下面,那么另一个将被剪裁.是否有一种有效/有效的方法来确保图像或page_desc始终可见并位于按钮上方?

Zso*_*agy 45

干得好:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@drawable/single_row"
    android:padding="12dip">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/page_image"
            android:layout_marginRight="6dip"
            android:layout_width="66dip"
            android:layout_height="66dip"
            android:layout_alignParentLeft="true"
            android:src="@drawable/no_photo" />
        <TextView
            android:id="@+id/page_name"
            style="@style/pulse_content"
            android:layout_alignTop="@id/page_image"
            android:layout_toRightOf="@id/page_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/page_desc"
            android:layout_below="@id/page_name"
            style="@style/pulse_content"
            android:layout_alignLeft="@id/page_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Principal Consultant" />
    </RelativeLayout>

    <Button
        android:id="@+id/follow_button"
        android:layout_marginTop="15dip"
        android:text="Follow"
        style="@style/follow_button" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

  • ??? 使用RelativeLayout的要点是AVOID必须嵌套LinearLayouts.:( (11认同)
  • 我没有看到只用一个RelativeLayout解决这种情况的方法.您不能将元素定义为低于2个其他元素.如果您可以在不嵌套ViewGroup的情况下解决此问题,请发布另一个答案.此外,它在哪里说避免嵌套是RelativeLayout的目的?它可以在许多情况下用于此,确实如此,但也有例外. (4认同)

Fuz*_*gic 6

这里的答案需要很少的改变.您的大部分布局都是正确的,但是,有一个选项可以搞乱一切.AlignParentXXXX通常会优先于LayoutXXXX选项的"错误"优先级.因此,通过将Button设置为AlignParentBottom,您告诉RelativeLayout不会在父布局大小中计算Button的大小.

您可以通过从Button中删除AlignParent ="true"来解决此问题.结果代码低于并经过测试.我相信,这个解决方案符合你的愿望.

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/single_row"
    android:padding="12dip">

    <ImageView
        android:id="@+id/page_image"
        android:layout_marginRight="6dip"
        android:layout_width="66dip"
        android:layout_height="66dip"
        android:layout_alignParentLeft="true"
        android:src="@drawable/no_photo" />
    <TextView
        android:id="@+id/page_name"
        style="@style/pulse_content"
        android:layout_alignTop="@id/page_image"
        android:layout_toRightOf="@id/page_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/page_desc"
        android:layout_below="@id/page_name"
        style="@style/pulse_content"
        android:layout_alignLeft="@id/page_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Principal Consultant" />
    <Button
        android:id="@+id/follow_button"
        android:layout_below="@id/author_image"
        android:layout_marginTop="15dip"
        android:text="Follow"
        style="@style/follow_button" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

当WrapContent在Parent上时,我遇到了许多与AlignParent相似的问题.如果没有为它做好准备,顶部和底部定位会产生不良行为.我发现,一般情况下,最好只使用其中一个(如果有的话)并将其余部分排在上面或下面.