Android Studio button positioning

≯℡__Kan透↙ 提交于 2021-02-07 10:23:24

问题


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.

EDIT: 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);

it wouldn't stick half out of the screen.


回答1:


Please check this SO Answer Changing position of a button.

Alternative Way, You can add this in your button(RelativeLayout)

android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"

Sample Demo

<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 If true, makes the bottom edge of this view match the bottom edge of the parent. Accommodates bottom margin.

android:layout_alignParentRight If true, makes the right edge of this view match the right edge of the parent. Accommodates right margin.

Both are boolean value, either "true" or "false".

I hope it will helps you .




回答2:


Use Linear Layout as parent Layout and following line in button

android:layout_gravity="bottom|right"



回答3:


Simply try this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="20dp"
    android:layout_marginRight="20dp"
    android:text="Bottom Right Button" />

Hope this work for you




回答4:


The Simple Code of Button to move its position is given below. You can actually change the dp size of the layout yourself. This is Very Simple Code To Move The direction of the button

Image Shows The Proof



来源:https://stackoverflow.com/questions/32051477/android-studio-button-positioning

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!