I want to put the button in the middle of Linear layout, what should I do?

Asked 2 years ago, Updated 2 years ago, 117 views

It outputs the screen to linear layout. I want to print a button in the middle of the screen But it's not as easy as you think No matter how you do it, the top is aligned in the middle...

The bottom is my xml, what should I do?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ImageButton android:id="@+id/btnFindMe" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|center_horizontal"
        android:background="@drawable/findme"></ImageButton>

</LinearLayout>

android xml layout

2022-09-22 22:31

1 Answers

If you want to print it out in the middle of the screen, you should not use Linear Layout.

Use Relative Layout instead. So android:layout_gravity="center_vertical|center_horizontal" Replace this part with android:layout_centerInParent="true" in RelativeLayout.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/RelativeLayout01" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <ImageButton android:id="@+id/btnFindMe" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@drawable/findme"></ImageButton>

</RelativeLayout>

Like this.


2022-09-22 22:31

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.