繁体   English   中英

Android:使用FrameLayout将按钮对齐到屏幕的右下角?

[英]Android: Align button to bottom-right of screen using FrameLayout?

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

"

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

我知道它有效,因为我正在使用它。 我知道为时已晚,但对其他人来说可能会派上用场,因为它在 google 上位居前列

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

一些使用 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>

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

可以使用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>

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

有两种方法可以做到这一点:

1) 使用框架布局

android:layout_gravity="bottom|right"

2) 使用相对布局

android:layout_alignParentRight="true"
android:layout_alignParentBottom="true" 
android:layout_gravity="bottom"

如果您想尝试使用java代码。 干得好 -

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

您可以向 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>

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

两种最常用的方法是:

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

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

android:layout_gravity="bottom|right"

你不能用 FrameLayout 做到这一点。

从规格:

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

“FrameLayout 旨在遮挡屏幕上的一个区域以显示单个项目。您可以向 FrameLayout 添加多个子项,但所有子项都固定在屏幕的左上角。”

为什么不使用RelativeLayout?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM