how to align text vertically center in android

后端 未结 5 1899
终归单人心
终归单人心 2021-02-02 06:14

I have arabic text, therefore I set gravity to right in order to start text from right side. Text starts from right now. But another issue is text starts to render from the top

相关标签:
5条回答
  • 2021-02-02 06:24

    Your TextView Attributes need to be something like,

    <TextView ... 
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical|right" ../>
    

    Now, Description why these need to be done,

     android:layout_width="match_parent"
     android:layout_height="match_parent"
    

    Makes your TextView to match_parent or fill_parent if You don't want to be it like, match_parent you have to give some specified values to layout_height so it get space for vertical center gravity. android:layout_width="match_parent" necessary because it align your TextView in Right side so you can recognize respect to Parent Layout of TextView.

    Now, its about android:gravity which makes the content of Your TextView alignment. android:layout_gravity makes alignment of TextView respected to its Parent Layout.

    Update:

    As below comment says use fill_parent instead of match_parent. (Problem in some device.)

    0 讨论(0)
  • 2021-02-02 06:27

    just use like this to make anything to center

     android:layout_gravity="center"
     android:gravity="center"
    

    updated :

    android:layout_gravity="center|right"
    android:gravity="center|right"
    

    Updated : Just remove MarginBottom from your textview.. Do like this.. for your textView

    <LinearLayout
            android:id="@+id/linearLayout5"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"  >
    
            <TextView
                android:id="@+id/textView2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center" 
                android:gravity="center|right"
                android:text="hello" 
                android:textSize="20dp" />
        </LinearLayout>
    
    0 讨论(0)
  • 2021-02-02 06:38

    In relative layout you need specify textview height:

    android:layout_height="100dp"
    

    Or specify lines attribute:

    android:lines="3"
    
    0 讨论(0)
  • 2021-02-02 06:40

    Try to put android:gravity="center_vertical|right" inside parent LinearLayout else as you are inside RelativeLayout you can put android:layout_centerInParent="true" inside your scrollView.

    0 讨论(0)
  • 2021-02-02 06:48

    The problem is the padding of the font on the textview. Just add to your textview:

    android:includeFontPadding="false"
    
    0 讨论(0)
提交回复
热议问题