Android Studio按钮定位 [英] Android Studio button positioning
问题描述
我正在尝试将按钮位置设置在屏幕的右下角.我已经尝试过了:
I'm trying to set button position to the bottom right corner of the screen. I've tried with this:
button.setX(maxX);
button.setY(maxY);
但是该按钮不可见,因为它不在屏幕上.
but the button is invisible because it's off the screen.
进行澄清.当我将按钮的位置设置为 maxX
和 maxY
时,我需要找到一种方法来保持按钮的布局.为了防止它越界.因此,即使我将其位置设置为:
To clarify. I need to find a way to keep my button WITHIN layout when I set its position to maxX
and maxY
. To prevent it from going out of bounds. So even if I set its position to something like:
button.setX(maxX - 10);
button.setY(maxY - 10);
它不会伸出屏幕一半.
推荐答案
请选中此答案更改按钮的位置.
替代方式,您可以将其添加到您的按钮(RelativeLayout)中
Alternative Way, You can add this in your button(RelativeLayout)
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
示例演示
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#AF3800"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
/>
android:layout_alignParentBottom
如果为true,则使该视图的底部边缘与父视图的底部边缘匹配.容纳底部边距.
android:layout_alignParentBottom
If true, makes the bottom edge of this view match the bottom edge of the parent. Accommodates bottom margin.
android:layout_alignParentRight 如果为true,则使该视图的右边缘与父视图的右边缘匹配.容纳右边距.
android:layout_alignParentRight If true, makes the right edge of this view match the right edge of the parent. Accommodates right margin.
两者均为布尔值,"true"或"false".
Both are boolean value, either "true" or "false".
我希望它会对您有所帮助.
I hope it will helps you .
这篇关于Android Studio按钮定位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!