【问题标题】:Android - can't get relative layout to align to parent bottomAndroid - 无法获得相对布局以与父底部对齐
【发布时间】:2015-07-23 06:47:28
【问题描述】:

我正在尝试在(GoogleMap 虽然我不确定它的相关性...)片段顶部添加一个自定义按钮。

我不希望它本身位于其下方 - 就像我可以使用 FrameLayoutLinearLayout 一样 - 但我希望按钮位于顶部其父布局透明的地图...

这是我(目前)在我的 xml 中拥有的:

<fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment"
    map:mapType="normal"
    tools:layout="@layout/activity_location">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" // THIS DOESNT WORK !!!
        android:background="#FC0" // for debug purposes
        android:gravity="center|bottom" >

        <Button
            android:id="@+id/location_order_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/small_margin"
            android:background="@drawable/nav_btn_send"
            android:text=""
            android:textColor="@color/black"
            android:textSize="@dimen/loginFontSize" />
    </RelativeLayout>

</fragment>

注意 'android:layout_alignParentBottom' 和 'android:background' 行上的 cmets...

这给了我这个结果:

现在...您可以通过代码告诉我我正在尝试将其与屏幕/地图底部对齐...但无论我做什么,它都会与顶部对齐,在 ActionBar 下方...

我已经尝试了一些我在 SO 上找到的其他方法,但这是迄今为止我想要的最接近的结果......

知道我做错了什么吗?

【问题讨论】:

    标签: android android-layout android-fragments


    【解决方案1】:

    您应该以这种方式在RelativeLayout 中定义您的片段:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
    
        android:background="#FC0"
        android:gravity="center|bottom" >
    
        <fragment
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            class="com.google.android.gms.maps.SupportMapFragment"
            map:mapType="normal"
            tools:layout="@layout/activity_location" />
    
        <Button
            android:id="@+id/location_order_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/small_margin"
            android:background="@drawable/nav_btn_send"
            android:text=""
            android:layout_alignParentBottom="true"
            android:textColor="@color/black"
            android:textSize="@dimen/loginFontSize" />
    </RelativeLayout>
    

    并且你应该在你想在父元素底部对齐的子元素中使用layout_alignParentBottom属性,在这种情况下,它是Button

    【讨论】:

      【解决方案2】:

      试试这个:

      <RelativeLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
      
      <fragment
          xmlns:map="http://schemas.android.com/apk/res-auto"
          xmlns:tools="http://schemas.android.com/tools"
          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_alignParentTop="true"
          class="com.google.android.gms.maps.SupportMapFragment"
          map:mapType="normal"
          tools:layout="@layout/activity_location"/>
      
          <Button
              android:id="@+id/location_order_btn"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_margin="@dimen/small_margin"
              android:background="@drawable/nav_btn_send"
              android:text=""
              android:textColor="@color/black"
              android:textSize="@dimen/loginFontSize"
              android:layout_centerHorizontal="true"
              android:layout_alignParentBottom="true"
              android:layout_marginBottom="10dp"/>
      
      </RelativeLayout>
      

      【讨论】:

      • 内部RelativeLayout是多余的
      • 是的,我知道,我认为您应该出于某种原因使用它。它在你的代码中。我已经编辑了我的帖子。
      【解决方案3】:

      对齐父底部是片段的无效布局参数。 尝试这个: 将相对布局的高度设置为 match_parent 并在相对布局的底部对齐按钮。

      <RelativeLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="#FC0">
      
          <Button
              android:id="@+id/location_order_btn"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_margin="@dimen/small_margin"
              android:layout_alignParentBottom="true"
              android:background="@drawable/nav_btn_send"
              android:text=""
              android:textColor="@color/black"
              android:textSize="@dimen/loginFontSize" />
      </RelativeLayout>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-16
        相关资源
        最近更新 更多