【问题标题】:Android: Align button to bottom-right of screen using FrameLayout?Android:使用FrameLayout将按钮对齐到屏幕的右下角?
【发布时间】:2011-05-06 15:15:21
【问题描述】:

我正在尝试将地图的缩放控件放在屏幕的右下角。我可以同时使用alignParentBottom="true"alignParentRight="true" 来使用RelativeLayout,但是使用Framelayout 我没有找到任何这样的属性。如何将其与屏幕右下角对齐?

【问题讨论】:

  • 在 Kotlin 中:parameters.gravity= Gravity.BOTTOM 或 Gravity.END

标签: android layout


【解决方案1】:

实际上这是可能的,尽管其他答案中说了些什么。如果您有一个FrameLayout,并且想要将一个子项定位到底部,您可以使用android:layout_gravity="bottom",这将使该子项与FrameLayout 的底部对齐。

我知道它有效,因为我正在使用它。我知道已经晚了,但它可能对其他人很方便,因为它在谷歌上排名靠前

【讨论】:

  • 嗨 byte1918,如果您认为这不是问题的正确答案,那么您非常欢迎 - 我认为这对每个人都有用 - 如果您发布正确的解决方案。或者至少解释一下是什么导致了这个错误的答案。谢谢。
  • 要对齐右下角,您可以使用 android:layout_gravity="bottom|right"
  • 如果它没有立即反映在预览中,请尝试从设计选项卡中进行设置。通常有效,
  • 我已经用 Textview 试过了,但它不起作用
【解决方案2】:

我也遇到了这种情况,并想出了如何使用 FrameLayout 来做到这一点。 以下输出由下面给出的代码生成。

              <FrameLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/contactbook_icon"
                        android:layout_gravity="center" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="140"
                        android:textSize="12dp"
                        android:textColor="#FFFFFF"
                        android:layout_gravity="bottom|right"
                        android:layout_margin="15dp" />
                </FrameLayout>

更改边距值以调整图像上的文本位置。有时删除边距可能会使文本离开视图。

【讨论】:

  • 最佳答案...谢谢,解决了一个长期存在的谜团。
【解决方案3】:

可以使用RelativeLayout实现

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

   <ImageView 
      android:src="@drawable/icon"
      android:layout_height="wrap_content"
      android:layout_width="wrap_content"
      android:layout_alignParentRight="true"
      android:layout_alignParentBottom="true" />

</RelativeLayout>

【讨论】:

  • 这是行不通的,如果在RelativeLayout 中使用wrap_content。因为无论如何,RelativeLayout 会在整个屏幕上缩放。
【解决方案4】:

设置 android:layout_gravity="bottom|right" 对我有用

【讨论】:

    【解决方案5】:

    两种方法:

    1) 使用框架布局

    android:layout_gravity="bottom|right"
    

    2) 使用相对布局

    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true" 
    

    【讨论】:

      【解决方案6】:
      android:layout_gravity="bottom"
      

      【讨论】:

      • 我们为什么要使用重力,有什么具体用途
      【解决方案7】:

      如果您想尝试使用 java 代码。给你-

          final LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, 
                                                   LayoutParams.WRAP_CONTENT);
          yourView.setLayoutParams(params);
          params.gravity = Gravity.BOTTOM; // set gravity
      

      【讨论】:

      • 正是我想要的。一点补充 - LayoutParams 应该来自 FrameLayout.LayoutParams。如果你想要它在右下角使用:params.gravity = Gravity.BOTTOM|Gravity.RIGHT;
      【解决方案8】:

      您可以在 FrameLayout 中添加一个不可见的 TextView。

          <TextView
              android:layout_width="wrap_content"
              android:layout_height="0dp"
              android:layout_weight="1"
              android:visibility="invisible"
              />
      
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="50dp"
              android:orientation="horizontal">
              <Button
                  android:id="@+id/daily_delete_btn"
                  android:layout_width="0dp"
                  android:layout_height="50dp"
                  android:layout_weight="1"
                  android:text="DELETE"
                  android:layout_gravity="center"/>
              <Button
                  android:id="@+id/daily_save_btn"
                  android:layout_width="0dp"
                  android:layout_height="50dp"
                  android:layout_weight="1"
                  android:text="SAVE"
                  android:layout_gravity="center"/>
          </LinearLayout>
      

      【讨论】:

        【解决方案9】:

        有很多方法可以使用android studio的activity.xml页面的约束小部件来做到这一点。

        两种最常用的方法是:

        1. 选择您的按钮视图并调整其边距。
        2. 转到“所有属性”,然后转到“layout_constraints”,然后选择
          • layout_constraintBottom_toBottomOf_parent
          • layout_constraintRight_toRightOf_parent

        【讨论】:

          【解决方案10】:

          使用bottom|right 作为您希望在父布局的右下位置对齐的元素的layout_gravity 值。

          android:layout_gravity="bottom|right"
          

          【讨论】:

            【解决方案11】:

            FrameLayout 无法做到这一点。

            来自规范:

            http://developer.android.com/reference/android/widget/FrameLayout.html

            “FrameLayout 的设计目的是在屏幕上屏蔽一个区域以显示单个项目。您可以将多个子项添加到 FrameLayout,但所有子项都固定在屏幕的左上角。”

            为什么不使用RelativeLayout?

            【讨论】:

            • 我想在左侧显示一个列表,与地图重叠。哦,RelativeLayout 是可能的,不是吗?好的,谢谢。
            • "但是所有的孩子都固定在屏幕的左上角" - 不一定是屏幕的顶部,你可以把它右对齐,右下对齐?
            • 是的,RelativeLayout 可以做到这一点。您可以在彼此的顶部布局视图。
            • 这实际上是正确的。正如您所提到的,OP 的要求是可以实现的,但当然视野有限。但说这是“无法实现”是错误的!
            猜你喜欢
            • 2019-01-05
            • 2013-05-20
            • 1970-01-01
            • 1970-01-01
            • 2015-03-17
            • 2017-02-01
            • 2013-11-09
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多