android - Centering a button in a linear layout -


using gravity on linearlayout centers button correctly:

    <linearlayout         android:layout_width="match_parent"         android:layout_height="match_parent"         android:orientation="vertical"         android:gravity="center">          <button         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="@string/new"         android:paddingleft="40dp"         android:paddingright="40dp"/>     </linearlayout> 

but using layout_gravity=center on button centers button horizontally , not vertically, why?

        <linearlayout             android:layout_width="match_parent"             android:layout_height="match_parent"             android:orientation="vertical">              <button             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="test"             android:paddingleft="40dp"             android:paddingright="40dp"             android:layout_gravity="center"/>         </linearlayout> 

enter image description here

i think layout_gravity can make button center in linearlayout.
helpful article: linearlayout gravity , layout_gravity explained
check simple code below. give button in center horizontal of linearlayout

<?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" >      <linearlayout         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:background="#f00"         android:orientation="vertical" >          <button             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_gravity="center"             android:text="button center hozizontal" />     </linearlayout>      <linearlayout         android:layout_width="match_parent"         android:layout_height="100dp"         android:background="#00f"         android:orientation="horizontal" >          <button             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_gravity="center"             android:text="button center vertical" />     </linearlayout>      <linearlayout         android:layout_width="match_parent"         android:layout_height="match_parent"         android:background="#0ff"         android:orientation="horizontal" >          <linearlayout             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:layout_gravity="center"             android:orientation="vertical" >              <button                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:layout_gravity="center"                 android:text="button center vertical/ horizontal" />         </linearlayout>     </linearlayout> </linearlayout> 

image screenshot

hope help.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -