【问题标题】:Align Relative Layout at bottom of Linear Layout in Parent在父级的线性布局底部对齐相对布局
【发布时间】:2017-06-13 17:34:18
【问题描述】:

我有线性布局,它充当相对布局的父级。相对布局由一些按钮和东西组成。我想在线性布局的底部对齐相对布局(包含按钮的面板)。线性布局仅由 ImageView 组成,之后相对布局应在底部对齐。

但是当我尝试在相对布局中设置 android:layout_alignParentBottom="true" 时,IDE 提示代码是错误的。我该如何实现这一点,请帮助

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

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="fill_parent"
        android:layout_height="250dp"
        android:scaleType="fitStart"
        android:src="@drawable/jellyfish" />

    <RelativeLayout

        android:layout_width="318dp"
        android:layout_height="164dp"
        android:layout_margin="5dp"
        android:background="@drawable/backgrad"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="20dp"
            android:layout_marginLeft="34dp"
            android:onClick="clickme"
            android:text="Button" />

        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginTop="16dp"
            android:layout_toLeftOf="@+id/button1"
            android:onClick="textclick"
            android:src="@drawable/text" />

        <Button
            android:id="@+id/button2"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/button1"
            android:layout_alignBottom="@+id/button1"
            android:layout_centerHorizontal="true"
            android:onClick="wmark"
            android:text="Save" />


    </RelativeLayout>

</LinearLayout>

【问题讨论】:

  • 像 "android:layout_width="318dp"" 这样的 FWIW 语句让我担心。你应该尽可能地做出相对的陈述,而不是绝对的陈述。这里的 wrap_content 有什么问题我不知道。
  • @SK9 是的,我会改变它。其实我很担心平板电脑等大型设备上的 UI 显示。下面会有很多空白,所以需要一些如何集中项目跨度>

标签: android android-layout user-interface


【解决方案1】:

可能重复:How to achieve alignParentBottom="true" property in LinearLayout

当您设置android:layout_alignParentBottom="true" 时,您正在尝试使用LinearLayout 的方法。但 LinearLayout 不提供alignParentBottom

副本中的答案说要替换

android:layout_alignParentBottom="true"

android:gravity="bottom"

【讨论】:

    【解决方案2】:

    将以下内容添加到您的父线性布局 xml:

    android:gravity="bottom"
    

    【讨论】:

    • 您是否尝试将其添加到您的父线性布局中(您已将其添加到问题中的 XML 中)?什么是没用?嗯......我怀疑这不会给你你想要的东西,因为你可能想要一个不同的重力图像子视图。也就是说,您很可能希望父级为 RelativeLayout,但这取决于您。
    • 我之前添加了完整的 xml,但不知何故它没有出现。即使添加了您在相对布局中提供的代码,(底部面板)相对布局也未在底部对齐
    • 再次,您是否尝试将其添加到您的父线性布局? (同样,不是相对布局。)
    • 我想要的是在底部以相对布局(具有不同背景)对齐项目。如何将其添加到父级解决此问题?
    • 再次,您是否尝试将其添加到您的父线性布局中?在撰写本文时,您的问题的两个答案都建议这样做。当您的父级是线性布局时,这是您可以做的最好的事情,并且必须在父级上设置。您似乎忽略了这个建议并做自己的事情。
    【解决方案3】:

    我已经回答了另一个这样的话题,解决方案是在你的相对布局上方放置一个权重为 1 的视图,如下所示:

        <View android:id="@+id/emptySpace"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        />
    

    【讨论】:

      【解决方案4】:

      尝试将您的 ImageView 包含在线性布局中并添加

      android:layout_alignParentBottom="true"
      

      在您的相对布局中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-01-05
        • 1970-01-01
        • 1970-01-01
        • 2021-10-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多