How to center a textview inside a linearlayout

前端 未结 11 2142
死守一世寂寞
死守一世寂寞 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:53
        <TextView
    ...
        android:layout_gravity="center"
    ....
    />
    
    0 讨论(0)
  • 2020-12-15 03:55

    Use Relative Layout, it always gives more accurate and flexible layout

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/ic_launcher" />
    </RelativeLayout>
    
    0 讨论(0)
  • 2020-12-15 03:58

    Set width of your textview to wrap_content, and set your LinearLayout to

    android:gravity="center_horizontal"
    
    0 讨论(0)
  • 2020-12-15 03:59

    None of the other answers worked for me. I had to use android:textAlignment="center"

    My layouts were as follows

    <LinearLayout...
        <RelativeLayout...
            <RelativeLayout....
                <TextView
                    android:layout_centerInParent="true"
                    android:textAlignment="center"
    
    0 讨论(0)
  • 2020-12-15 04:04

    Try this

    simple i have tried to many answer but all answers are not working ......finally this methods works for me

                         <LinearLayout
                            android:layout_below="@+id/iv_divider"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:orientation="vertical"
                            android:layout_marginTop="@dimen/_10dp">
                            <TextView
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:text="@string/mycredits"
                                android:textColor="@color/colorGray"
                                android:gravity="center"
                                android:textSize="@dimen/_25dp" />
                        </LinearLayout>
    
    0 讨论(0)
提交回复
热议问题