문제

i have problem when set text gravity to center it's not appear in the middle ?!

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/AppBaseTheme"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

 <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="Large Text Large Text Large Text Large Text Large Text Large Text"
    android:layout_gravity="center"  
    android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>
도움이 되었습니까?

해결책

If you want the whole TextView to be centered (which would then center the text) use android:layout_gravity

 <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="Large Text Large Text Large Text Large Text Large Text Large Text"
    android:layout_gravity="center"   // Here
    android:textAppearance="?android:attr/textAppearanceLarge" />

If this doesn't fix the problem then please post the full xml as you may have other Views/properties causing the issue.

다른 팁

use width to fill parent with the rest

android:layout_width="fill_parent"

I think it just look that way in IDE try to run your app and test. Happened to me as well but it turned out to be an IDE problem, so just check it out.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top