How to center a textview inside a linearlayout

前端 未结 11 2141
死守一世寂寞
死守一世寂寞 2020-12-15 03:05

I have the following layout i\'d like to make the textview appear in the center and middle of the view. How can i acheive this?

I\'ve tried adjusting the various gra

相关标签:
11条回答
  • 2020-12-15 03:38

    Add android:gravity="center_horizontal" to your LinearLayout and alter your TextView's layout_width as android:layout_width="wrap_content"

    <?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:gravity="center_horizontal"
        android:background="@drawable/carefreebgscaledalphajpg" >
    
    
    
        <TextView
            android:id="@+id/textviewdatefornocallspage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="You have no calls for "
            android:textSize="25sp" />
    
    </LinearLayout>
    
    0 讨论(0)
  • 2020-12-15 03:38

    If in linearlayout your orientation vertical, you can put the textview in the "horizontal center" by android:layout_gravity="center". For centering textview vertically you need to set layout_height of textview to match_parent and set android:gravity to "center". If you want to use more than one view in this layout, you should use RelativeLayout and set android:layout_centerInParent="true".

    0 讨论(0)
  • 2020-12-15 03:39

    set the gravity to center in your linearLayout tag :

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
         android:gravity="center"
        android:background="@drawable/carefreebgscaledalphajpg" >
    

    or use a RelativeLayout like this :

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/carefreebgscaledalphajpg" >
    
    
    
        <TextView
            android:id="@+id/textviewdatefornocallspage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="You have no calls for "
            android:textSize="25sp" />
    
    </RelativeLayout>
    
    0 讨论(0)
  • 2020-12-15 03:40

    Sounds like you want android:layout_gravity for the TextView

    see docs: developer.android.com

    0 讨论(0)
  • 2020-12-15 03:49

    set android:gravity="center" in your Linear Layout

    0 讨论(0)
  • 2020-12-15 03:51

    Set android:gravity="center" attribute for TextView inside linear layout and keep the text view's height and width as wrap_content.

    This Solution worked for me.

    0 讨论(0)
提交回复
热议问题