Home > front end >  XML text alignment
XML text alignment

Time:07-11

Hellow i've a problem wiith my code, i don't know why te text isn't centered

this is the code:

<?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">

    <TextView
        android:id="@ id/name_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="@string/press"
        android:textColor="@color/purple_200"
        android:textSize="30sp" />
</Linear Layout>

the result photo code photo

CodePudding user response:

<?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">

    <TextView
        android:id="@ id/name_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/press"
        android:textColor="@color/purple_200"
        android:textSize="30sp" />
</LinearLayout> 
  1. Your ending bracket had an unnecessary space </Linear Layout>
  2. Use layout_gravity instead of gravity:

Layout Gravity affects your position in the superview. Gravity affects the position of your subviews within you.

  • Related