【问题标题】:Android: Align Parent Bottom + Bottom marginAndroid:对齐父底部+底部边距
【发布时间】:2012-11-06 15:53:15
【问题描述】:

我使用了相对布局,我想将按钮设置在屏幕底部,但是这会将它全部放在底部,我希望有一些边距,所以它的末尾之间有一些空间屏幕/视图和按钮。但是,无论我做什么,按钮边距都出于某种原因在 2.1+ 上没有任何作用。相对布局包含一个背景,所以我不能只保留边距。

有人知道解决这个问题的方法吗?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="0dp"
android:background="@drawable/background" >

    <Button
        android:id="@+id/confirm_mobile_button_next"
        android:layout_width="fill_parent"
        android:layout_height="40dip"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="false"
        android:layout_centerHorizontal="false"
        android:layout_centerInParent="false"
        android:layout_margin="15dp"
        android:background="@drawable/button_shape_selector"
        android:paddingLeft="10dip"
        android:paddingRight="10dip"
        android:text="@string/confirm_mobile_no_continue"
        android:textColor="@color/white"
        android:textStyle="bold" />

</RelativeLayout>

【问题讨论】:

    标签: android android-relativelayout margins


    【解决方案1】:

    @franny zhao 建议的唯一可行解决方案如下。

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <FrameLayout
            android:id="@+id/fl_layout"
            android:layout_alignParentBottom="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/tv_layout"
                android:text="hello world"
                android:layout_marginBottom="50dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </FrameLayout>
    </RelativeLayout>
    

    如果您按照其他人的建议为底部对齐的视图添加填充,则视图背景也会扩展。如果你有彩色背景,那么视图看起来就像粘在底部一样。填充和边距完全不同,填充是视图的一部分,但边距在视图之间留下空间。

    【讨论】:

      【解决方案2】:

      您可以做的另一件事是放置一个与RelativeLayout 底部对齐的View,并将其高度设置为您想要使用的底部边距(或简单地为layout_marginBottom 指定一个值)像这样:

       <RelativeLayout
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"        
          > 
          <ImageView
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:src="@drawable/some_image"
              android:adjustViewBounds="true"
      
               />
          <TextView
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:text="Some Overlay"
              android:padding="3dip"
              android:gravity="center_vertical|center_horizontal"
      
              android:layout_above="@+id/empty_view"
              android:layout_marginBottom="35dip"
              />
          <View 
              android:id = "@+id/empty_view"
              android:layout_height = "30dip"
              android:layout_width = "match_parent"
              android:visibility="invisible"
              android:layout_alignParentBottom="true"
              />
      
          </RelativeLayout>
      

      此示例用ImageView 填充RelativeLayout,并在ImageView 上方放置一个TextView

      【讨论】:

      • 不要多次列出带有+ 的ID。您应该只在第一个实例上使用 @+id,然后只在 XML 文件的其他地方使用 @id。因此,在填充 View 开始后您有 android:id = "@+id/empty_view" 的地方,您不应该有 +
      • 使用空间而不是不可见的视图
      • @DaveHaigh 目前我只能为您提供这个问题的答案:stackoverflow.com/questions/11160954/… + 的含义有点像java 关键字new。如果您在 XML 中多次使用 +,您实际上是在告诉系统创建另一个新 id。我认为该系统足够智能,实际上不会创建另一个新系统,但这不是重点;如果将来系统发生变化,您的代码就会中断。如果您使用正确的设置打开它,我认为这也会被 lint 拉起。
      【解决方案3】:

      您可以使用 ViewGroup(例如,FrameLayout 或 LinearLayout)来包装视图。在外部 ViewGroup 中设置 alignParentBottom,marginBottom 可以在内部 View 中工作。

      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content">
      
          <FrameLayout
              android:id="@+id/fl_layout"
              android:layout_alignParentBottom="true"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content">
              <TextView
                  android:id="@+id/tv_layout"
                  android:text="hello world"
                  android:layout_marginBottom="50dp"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content" />
          </FrameLayout>
      </RelativeLayout>
      

      【讨论】:

        【解决方案4】:

        你可以使用 translateY 属性

        translateY="-16dp"
        

        最终代码

        <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="0dp"
            android:background="@drawable/background">
        
            <Button
                android:id="@+id/confirm_mobile_button_next"
                android:layout_width="fill_parent"
                android:layout_height="40dip"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="false"
                android:layout_centerHorizontal="false"
                android:layout_centerInParent="false"
                android:layout_margin="15dp"
                android:background="@drawable/button_shape_selector"
                android:paddingLeft="10dip"
                android:paddingRight="10dip"
                android:text="@string/confirm_mobile_no_continue"
                android:textColor="@color/white"
                android:textStyle="bold"
                android:translateY="-16dp" />
        
        </RelativeLayout>
        

        【讨论】:

          【解决方案5】:

          我认为最好的方法是设置:

          <...
          android:layout_alignParentBottom="true" />
          

          然后在java代码后面:

          Button confirm_mobile_button_next = (Button)findViewById(R.id.confirm_mobile_button_next)
          confirm_mobile_button_next.setTransitionY(-THE_VALUE) or setTransitionX(-THE_VALUE)
          

          【讨论】:

            【解决方案6】:

            我认为最好的方法是在 XML 中设置 android:layout_alignParentBottom 然后在java代码后面:

            Button confirm_mobile_button_next = (Button)findViewById(R.id.confirm_mobile_button_next)
            

            confirm_mobile_button_next.setTransitionY(-THE_VALUE) or setTransitionX(-THE_VALUE)
            

            【讨论】:

              【解决方案7】:

              由于我偶然发现了这个问题,并且没有找到适合我的情况的答案,我开始思考半秒钟并通过在 RelativeLayout 示例中的视图上设置负边距来解决它:

              <RelativeLayout
                  android:layout_width="match_parent"
                  android:layout_height="match_parent">
                  <ImageView
                  android:layout_width="48dp"
                  android:layout_height="48dp"
                      android:layout_marginTop="-8dp"
                      android:layout_marginStart="-8dp">
              </RelativeLayout>
              

              这应该对某些人有用。

              【讨论】:

                【解决方案8】:

                这是另一种选择。如果要设置子级的边距而不是父级的内边距,则应将android:layout_marginTop 的值设置为所需边距的两倍,然后将android:layout_centerVertical 设置为true。上边距被赋予期望值的两倍以补偿下边距。这样子视图周围的顶部和底部边距将相等。

                <Button
                    android:id="@+id/confirm_mobile_button_next"
                    android:layout_width="fill_parent"
                    android:layout_height="40dip"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentLeft="false"
                    android:layout_centerVertical="true"
                    android:layout_centerInParent="false"
                    android:layout_marginLeft="15dp"
                    android:layout_marginRight="15dp"
                    android:layout_marginTop="30dp"
                    android:background="@drawable/button_shape_selector"
                    android:paddingLeft="10dip"
                    android:paddingRight="10dip"
                    android:text="@string/confirm_mobile_no_continue"
                    android:textColor="@color/white"
                    android:textStyle="bold" />
                

                它会给你同样的结果。

                【讨论】:

                  【解决方案9】:

                  这样试试:

                  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                           xmlns:tools="http://schemas.android.com/tools"
                           android:layout_width="fill_parent"
                           android:layout_height="fill_parent" >
                  
                  <Button
                      android:id="@+id/button1"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_alignParentBottom="true"
                      android:layout_centerHorizontal="true"
                      android:layout_marginBottom="17dp"
                      android:text="Button" />
                  

                  【讨论】:

                    【解决方案10】:

                    您可以简单地向 RelativeLayout 添加填充,而不是向 Button 添加边距,例如android:paddingBottom="15dp".

                    一般来说,我总是使用 API Level 8 设置在 Exclipse 预览中测试我的布局。这为大多数设备(包括 ICS 和 JB)提供了相当准确的结果。

                    【讨论】:

                    • 这对我没有用,因为我使用的 MapView 应该覆盖所有屏幕。我只是使用android:layout_alignParentBottom="true" 添加具有所需高度的空白TextView 并将所需的对象(在本例中为Button)放在上面。
                    • 这个方案在textview/button有背景的时候是无效的,因为它的大小受paddingBottom影响,看起来会很恐怖。最好在底部放置一个不可见的View
                    • 这是真的,@voghDev。我想最好的建议是在应用任何随机填充或边距之前考虑布局,因为两者从根本上不同,尽管它们通常会产生非常相似的输出。 Stackoverflow 上有一篇关于填充和边距之间差异的好文章:stackoverflow.com/questions/5958699/…。虽然它涵盖了 CSS,但同样的原则也适用于 Android 的布局引擎。
                    • 设置视图的 Padding 与设置视图的 Margin 不同。 Padding 定义了视图边界和视图内部内容之间的空间,而 Margin 定义了视图边界周围的空间。这种区别对于 Button 和 TextView 等视图很重要,在这些视图中设置 Padding 而不是 Margin 可能会导致视图内的文本被截断。对于上面定义的Button 尤其如此,因为layout_height 定义了内容高度+填充。更多信息在这里:stackoverflow.com/a/4619943/2326740
                    • 谢谢你!尽管就 Android 源而言,这似乎是一种解决方法,而不是解决方案
                    猜你喜欢
                    • 1970-01-01
                    • 1970-01-01
                    • 2012-08-13
                    • 2014-07-19
                    • 1970-01-01
                    • 2015-12-30
                    • 2020-12-28
                    • 2015-12-02
                    • 2016-10-05
                    相关资源
                    最近更新 更多