簡體   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