简体   繁体   中英

How to align the buttons at the center of the screen in android

I have two buttons. I want to align them at the center of the screen,. I tried doing.. but i'm not able to do it.

fragment_main.xml :

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.assignment_1.MainActivity$PlaceholderFragment" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button1" 
        android:id="@+id/button1"
        android:onClick="Display"/>
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_toRightOf="@+id/button1"
        android:text="@string/button2" 
        android:id="@+id/button2"
        android:onClick="opaquemessage"/>
    <TextView 
        android:id="@+id/display_mes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"/>

</RelativeLayout>

You just add this line into your button that's all.... This one is for your Relative layout

    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" 

suppose you will use the linear layout means use this below

    android:layout_gravity="center"

Just try this I'm using linear layout here

 <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" 
    android:layout_gravity="center">
        <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/button1" 
                android:id="@+id/button1"
                android:onClick="Display"
        android:gravity="center"/>
            <Button
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/button1"
                android:layout_toRightOf="@+id/button1"
                android:text="@string/button2" 
                android:id="@+id/button2"
                android:onClick="opaquemessage"
        android:gravity="center"/>
            <TextView 
                android:id="@+id/display_mes"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
        android:gravity="center"/>
        </LinearLayout>

you can add to your button code :

 android:layout_gravity="center"

as in

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="Button" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM