【问题标题】:Align a button below the bottom of the screen对齐屏幕底部下方的按钮
【发布时间】:2016-06-06 22:33:05
【问题描述】:

目前我的代码在屏幕底部放置了一个按钮:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="bottom"
    android:layout_alignParentBottom="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center|bottom">

        <ImageButton
            android:id="@+id/button1"
            android:layout_width="95dp"
            android:layout_height="95dp"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:src="@drawable/main_button"
            />
    </LinearLayout>
</RelativeLayout>

但是,我希望将其稍微对齐到屏幕底部下方,因此按钮的一小部分会被剪掉(比如 20%)。最终,我的最终结果是,当我点击并向上拖动按钮时,它会向上移动并显示整个按钮,但在此之前,它的底部被剪掉了。


关于如何将按钮对齐到屏幕底部有很多问题,但我似乎找不到任何将其对齐到屏幕底部下方的问题。

有什么方法可以这样做,还是我没有使用正确的搜索词?


编辑:从答案中,我尝试了 android:translationY="20dp" 但它没有用,但我尝试了 android:layout_marginBottom="-20dp" 并且它对我有用。

【问题讨论】:

  • 如果你可以将其与底部对齐,那么你可以添加一个上边距以将其推离屏幕

标签: android android-layout button android-button


【解决方案1】:

您可以将android:translationY="Xdp" 添加到您的按钮以将其向下移动 X dp。

或者,您可以创建两个LinearLayouts。一个填充屏幕并保持其他组件,另一个包含按钮。然后,您可以添加android:layout_below="@id/layout0"android:margin_top="Xdp" 来控制带有按钮的布局应该在屏幕下方的位置。

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="bottom"
    android:layout_alignParentBottom="true">

        </LinearLayout android:id="@+id/layout0"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_below="@id/layout0"
            android:margin_top="Xdp"
            android:gravity="center|bottom">

                <ImageButton
                    android:id="@+id/button1"
                    android:layout_width="95dp"
                    android:layout_height="95dp"
                    android:layout_centerInParent="true"
                    android:gravity="center"
                    android:src="@drawable/main_button"/>
       </LinearLayout>
</RelativeLayout>

【讨论】:

    【解决方案2】:

    您可以翻译按钮:

    android:translationY="20dp"
    

    在您的 xml 中,这会将视图向下移动 20 dp。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-12
      • 2012-09-27
      • 2019-06-18
      • 1970-01-01
      • 2021-05-10
      • 1970-01-01
      相关资源
      最近更新 更多