简体   繁体   English

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

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

I am trying to put the zoom controls of the map on the bottom right corner of screen.我正在尝试将地图的缩放控件放在屏幕的右下角。 I could do it with RelativeLayout using both alignParentBottom="true"<\/code> and alignParentRight="true"<\/code> , but with Framelayout I did not find any such attributes.我可以同时使用alignParentBottom="true"<\/code>和alignParentRight="true"<\/code>使用 RelativeLayout 来做到这一点,但是使用 Framelayout 我没有找到任何此类属性。 How do I align it to the bottom-right of screen?如何将其与屏幕右下角对齐?

"

Actually it's possible, despite what's being said in other answers.实际上这是可能的,尽管其他答案中说了些什么。 If you have a FrameLayout , and want to position a child item to the bottom, you can use android:layout_gravity="bottom" and that is going to align that child to the bottom of the FrameLayout .如果您有一个FrameLayout ,并且想要将一个子项android:layout_gravity="bottom"在底部,您可以使用android:layout_gravity="bottom"并将该子项与FrameLayout的底部对齐。

I know it works because I'm using it.我知道它有效,因为我正在使用它。 I know is late, but it might come handy to others since this ranks in the top positions on google我知道为时已晚,但对其他人来说可能会派上用场,因为它在 google 上位居前列

I also ran into this situation and figured out how to do it using FrameLayout.我也遇到了这种情况,并想出了如何使用 FrameLayout 做到这一点。 The following output is produced by the code given below.以下输出由下面给出的代码产生。

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

Change the margin value to adjust the text position over the image.更改边距值以调整图像上的文本位置。 Removing margin might make the text to go out of the view sometimes.删除边距有时可能会使文本从视图中消失。

It can be achieved using RelativeLayout可以使用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"

Two ways to do this:有两种方法可以做到这一点:

1) Using a Frame Layout 1) 使用框架布局

android:layout_gravity="bottom|right"

2) Using a Relative Layout 2) 使用相对布局

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

If you want to try with java code.如果您想尝试使用java代码。 Here you go -干得好 -

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

you can add an invisible TextView to the FrameLayout.您可以向 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>

There are many methods to do this using constraint widget of the activity.xml page of android studio.有很多方法可以使用 android studio 的 activity.xml 页面的约束小部件来做到这一点。

Two most common methods are:两种最常用的方法是:

  1. Select your button view and adjust it's margins.选择您的按钮视图并调整它的边距。
  2. Go to "All attributes",then to "layout_constraints",then select转到“所有属性”,然后转到“layout_constraints”,然后选择
    • layout_constraintBottom_toBottomOf_parent layout_constraintBottom_toBottomOf_parent
    • layout_constraintRight_toRightOf_parent layout_constraintRight_toRightOf_parent

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

android:layout_gravity="bottom|right"

You can't do it with FrameLayout.你不能用 FrameLayout 做到这一点。

From spec:从规格:

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

"FrameLayout is designed to block out an area on the screen to display a single item. You can add multiple children to a FrameLayout, but all children are pegged to the top left of the screen." “FrameLayout 旨在遮挡屏幕上的一个区域以显示单个项目。您可以向 FrameLayout 添加多个子项,但所有子项都固定在屏幕的左上角。”

Why not to use RelativeLayout?为什么不使用RelativeLayout?

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

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