Android Studio 按钮定位

标签 android android-studio position bounds

我正在尝试将按钮位置设置到屏幕的右下角。我试过这个:

button.setX(maxX);
button.setY(maxY);

但是按钮是不可见的,因为它在屏幕之外。

编辑:澄清。当我将按钮的位置设置为 maxXmaxY 时,我需要找到一种方法使按钮保持在布局内。以防止它越界。所以即使我将其位置设置为:

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/

相关文章:

android - EditText.setMaxLines() 以编程方式不起作用

android - 为什么没有找到处理Intent的Activity?

java - 将 Java 文件转换为 Kotlin 现在无法编译 - "Internal compiler error"

安卓工作室 : sound notification on build success

来自模型 View 矩阵的 openGL 位置和方向

android - 如何在不使用计算机的情况下读取设备上的数据库文件

java - 我想显示一个变量值,其中变量名称与连接的字符串匹配

android - 在带有外部 C++ 源的 Android Studio 中使用 NDK

html - 每当我用来固定标题的位置时,使用 css 设置网页样式时,它的宽度就会减小。我该如何解决这个问题?

c# - 如何设置MediaElement的位置?