2012-11-20 11 views
21

Mam następujący układ, który chciałbym, aby widok tekstu pojawił się pośrodku i środku widoku. Jak mogę to osiągnąć?Jak wyśrodkować widok tekstowy wewnątrz tabeli liniowej

Próbowałem dostosować różne atrybuty grawitacji, patrząc na układ w widoku graficznym, ale nic nie wydaje się go zmieniać. Chciałbym zobaczyć tekst w centrum, który zrobiłem, ale w połowie widoku.

dzięki matt.

<?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:background="@drawable/carefreebgscaledalphajpg" > 



    <TextView 
     android:id="@+id/textviewdatefornocallspage" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:text="You have no calls for " 
     android:textSize="25sp" /> 

</LinearLayout> 

Odpowiedz

38

ustaw grawitację na cente R w tagu linearLayout:

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

lub użyć RelativeLayout takiego:

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

dzięki działa dobrze teraz – turtleboy

+1

Rozwiązanie RelativeLayout pracował dla mnie, ale w TextView musiałem użyć 'wrap_content' dla obu szerokości i wysokości –

+0

@Jose_GD: w obu przypadkach zadziała to dla ciebie :). w przypadku użycia atrybutu 'match_parent' dla szerokości, po prostu dodaj' layout_gravity = "center" '. To wszystko ;) – Houcine

0

Dodaj android:gravity="center_horizontal" do LinearLayout i zmieniać swoją TextView „s layout_width jak 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

ustawić szerokość swojego TextView do wrap_content swojej TextView i ustawić LinearLayout do android:gravity=center_horizontal

0
<TextView 
... 
    android:layout_gravity="center" 
.... 
/> 
0

Jeśli w liniowym ustawieniu orientacji pionowej, możesz umieścić widok tekstowy w "centrum poziomym" przez android:layout_gravity="center". Aby wyśrodkować tekst w pionie, musisz ustawić height_height widoku tekstowego na match_parent i ustawić android: gravity na "center". Jeśli chcesz użyć więcej niż jednego widoku w tym układzie, powinieneś użyć RelativeLayout i ustawić android:layout_centerInParent="true".

0

Zastosowanie względna Układ, to zawsze daje dokładniejsze i bardziej elastyczne dopasowanie

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

Ustaw android:gravity="center" w układzie liniowym i utrzymać wysokość i szerokość widoku tekstowego jako wrap_content.

To rozwiązanie sprawdziło się u mnie.

2

Spróbuj

prosty Próbowałem wielu odpowiedzi, ale wszystkie odpowiedzi nie działa ...... w końcu to metody pracuje dla mnie

     <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> 
Powiązane problemy