[英]Align a button below the bottom of the screen
Currently my code places a button at the bottom of the screen: 目前,我的代码在屏幕底部放置了一个按钮:
<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>
However, I wish to align it slightly below the bottom of the screen, so a small part of the button is clipped off (Say, 20%). 但是,我希望将其对齐到屏幕底部的下方,以便将按钮的一小部分剪掉(例如20%)。 Ultimately, my end result is that when I tap and drag the button up, it will move up and reveal the entire button, but before that, the bottom part of it is clipped.
最终,我的最终结果是,当我点击并向上拖动按钮时,它将向上移动并显示整个按钮,但是在此之前,它的底部被剪切了。
There are many questions on how to align a button to the bottom of a screen, but I can't seem to find any that aligns it to below the bottom of the screen. 关于如何将按钮与屏幕底部对齐有很多问题,但是我似乎找不到任何将按钮与屏幕底部对齐的东西。
Is there any way to do so, or am I not using the correct search terms? 有什么方法可以这样做,还是我使用的搜索词不正确?
EDIT: From the answers, I tried android:translationY="20dp" and it did not work, but I tried android:layout_marginBottom="-20dp" and it worked for me. 编辑:从答案,我尝试了android:translationY =“ 20dp”,它不起作用,但是我尝试了android:layout_marginBottom =“-20dp”,它对我有用。
You can add android:translationY="Xdp"
to your button to move it down by X dp. 您可以将
android:translationY="Xdp"
到按钮中,以将其向下移动X dp。
Alternatively you can create two LinearLayouts
. 或者,您可以创建两个
LinearLayouts
。 One to fill the screen and hold other components and another that contains the button. 一个填充屏幕并容纳其他组件,另一个包含按钮。 You can then add
android:layout_below="@id/layout0"
and android:margin_top="Xdp"
to control how far below the screen the layout with the button should be. 然后,您可以添加
android:layout_below="@id/layout0"
android:margin_top="Xdp"
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>
You can translate the button: 您可以翻译按钮:
android:translationY="20dp"
in your xml, that's moves the view 20 dp's down. 在您的xml中,将视图向下移动20 dp。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.