我正在尝试将按钮位置设置到屏幕的右下角。我试过这个:
button.setX(maxX);
button.setY(maxY);
但是按钮是不可见的,因为它在屏幕之外。
编辑:澄清。当我将按钮的位置设置为 maxX
和 maxY
时,我需要找到一种方法使按钮保持在布局内。以防止它越界。所以即使我将其位置设置为:
button.setX(maxX - 10);
button.setY(maxY - 10);
它不会伸出屏幕的一半。
最佳答案
请查看此 SO 答案 Changing position of a 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
如果为真,则使该 View 的底边与父 View 的底边相匹配。容纳下边距。
android:layout_alignParentRight 如果为真,则使该 View 的右边缘与父 View 的右边缘匹配。容纳右边距。
都是 bool 值,要么是“真”,要么是“假”。
希望对您有所帮助。
关于Android Studio 按钮定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32051477/