Frage

Wie kann ich zentriert den Text horizontal und vertikal in einem TextView, so dass es genau in der Mitte des TextView in Android erscheint?

War es hilfreich?

Lösung

Ich nehme an, Sie XML-Layout verwenden.

<TextView  
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center"
    android:text="@string/**yourtextstring**"
/>

Sie können auch die Schwerkraft center_vertical oder center_horizontal nach Ihrem Bedarf verwendet werden.

und als @stealthcopter kommentiert in Java: .setGravity(Gravity.CENTER);

Andere Tipps

android:gravity="center" 

Dies wird den Trick

Sie können auch dynamisch mit ein:

textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
android:layout_centerInParent="true"

Das funktioniert, wenn sie mit einem RelativeLayout verwendet, bei denen die Höhe des Layouts und Breite festgelegt werden wrap_content.

Sie können auch die Kombination verwenden:

android:gravity="left|center"

Wenn dann Textview Breite mehr als „fill_parent“ ist der Text immer noch nach links ausgerichtet wird (nicht zentriert ist, wie mit der Schwerkraft setzt nur auf „Mitte“).

Anwenden Dichte:

TextView txtView = (TextView) findViewById(R.id.txtView);
txtView.setGravity(Gravity.CENTER_HORIZONTAL);

Für vertikal:

txtView.setGravity(Gravity.CENTER_VERTICAL);

In XML:

<TextView      
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center"
    android:text="@string/Hello_World"        
/>

Wenn Sie mit Tablelayout sicherstellen, dass die Schwere der TableRows zum Zentrum zu setzen, auch. Sonst wird es nicht funktionieren. Wenigstens hat es nicht mit mir arbeiten, bis ich die Schwere des TableRow auf Mitte.

Zum Beispiel wie folgt aus:

<TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center">        
    <TextView android:text="@string/chf" android:id="@+id/tv_chf" android:layout_weight="2" android:layout_height="wrap_content" android:layout_width="fill_parent" android:gravity="center"></TextView>        
</TableRow>

Es gibt zwei Möglichkeiten, dies zu tun.

Die erst im XML-Code. Sie müssen die Aufmerksamkeit auf die Gravity Attribut zahlen. Sie können auch dieses Attribut in dem Grafik-Editor finden; es kann als XML-Editor einfacher sein.

<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center_vertical|center_horizontal"
    android:text="Your Text"
/>

Für Ihr spezielles Szenario werden die Werte der Schwerkraft werden sein:

center_vertical|center_horizontal

Im grafischen Editor werden Sie alle möglichen Werte finden, auch ihre Ergebnisse sehen.

Sie müssen auf Textview Gravity (Mitte Horizontal- und Vertikal-Center) wie folgt aus:

android:layout_centerHorizontal="true"   

und

android:layout_centerVertical="true"

Und dynamisch mit:

textview.setGravity(Gravity.CENTER);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);

Meiner Meinung nach,

android:gravity="center"

ist besser als,

android:layout_centerInParent="true"

das ist besser als,

android:layout_centerHorizontal="true"
android:layout_centerVertical="true"

zumindest für Formatierung von Text.

Für Linear Layout: In XML so etwas wie diese verwenden

<TextView  
    android:id="@+id/textView1"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center_vertical|center_horizontal"
    android:text="Your Text goes here"
/>

so etwas wie dies zur Laufzeit tun in Ihrer Tätigkeit nutzen

TextView textView1 =(TextView)findViewById(R.id.texView1);
textView1.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);

Für Relative Layout: in XML ein Ding wie diese verwenden

<TextView  
    android:id="@+id/textView1"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_centerInParent="true"
    android:text="Your Text goes here"
/>

so etwas wie dies zur Laufzeit tun in Ihrer Tätigkeit nutzen

TextView textView1 =(TextView)findViewById(R.id.texView1);
RelativeLayout.LayoutParams layoutParams = RelativeLayout.LayoutParams)textView1.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
textView1.setLayoutParams(layoutParams);

Verwenden Sie in der XML-Datei.

Layout-Datei

<TextView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:text="@string/stringtext"/>

oder:

Verwenden Sie diese innerhalb der Java-Klasse

TextView textView =(TextView)findViewById(R.id.texviewid);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);

Verwenden Sie diese für eine relative Layout

android:layout_centerInParent="true"

und für anderes Layout

android:gravity="center" 

Während der Schwerkraft für Textview arbeitet mit, gibt es eine alternative Methode in API-Ebene implementiert 17 -

textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);

Sie kennen den Unterschied nicht, aber es funktioniert auch. Allerdings nur für die API-Level 17 oder höher.

Wenn die TextView's Höhe und Breite wrap content werden dann der Text innerhalb des TextView immer zentriert sein. Aber wenn die TextView's Breite match_parent und Höhe ist match_parent oder wrap_content dann müssen Sie den folgenden Code schreiben:

RelativeLayout:

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center" 
        android:text="Hello World" />

</RelativeLayout>

Linearlayout:

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Hello World" />

</LinearLayout>

In RelativeLayout, wird es mit ihm schön.

Und noch ein Button und alles, was Sie hinzufügen können.

Die folgenden funktioniert gut für mich.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ff314859"
    android:paddingLeft="16dp"
    android:paddingRight="16dp">
    <TextView 
        android:id="@+id/txt_logo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="your text here"
        android:textSize="30dp"
        android:gravity="center"/>

        ...other button or anything else...

</RelativeLayout>

Der einfachste Weg (die überraschenderweise nur in den Kommentaren erwähnt, also warum ich als Antwort bin Entsendung) ist:

textview.setGravity(Gravity.CENTER)

Sie können nur die gravity Ihrer Textview in CENTER gesetzt.

Wenn Sie versuchen, Text zu zentrieren auf einem TableRow in einem Tablelayout, hier ist, wie ich dieses Ziel erreicht:

<TableRow android:id="@+id/rowName"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:padding="5dip" >
    <TextView android:id="@+id/lblSomeLabel"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:gravity="center"
              android:layout_width="0dp"
              android:layout_weight="100"
              android:text="Your Text Here" />
</TableRow>

Hier ist meine Antwort, die ich in meiner app benutzt hatte. Es zeigt Text in der Mitte des Bildschirms angezeigt.

<TextView
    android:id="@+id/txtSubject"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/subject"
    android:layout_margin="10dp"
    android:gravity="center"
    android:textAppearance="?android:attr/textAppearanceLarge" />

Wenn Sie mit Relative Layout:

  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/stringname"
    android:layout_centerInParent="true"/>

Wenn Sie mit Linearlayout

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/stringname"
    android:layout_gravity="center"/>

Wie viele Antworten vorschlagen oben funktioniert.

android:gravity="center"

Wenn Sie es zum Zentrum nur vertikal:

android:gravity="center_vertical"

oder nur horizontal:

android:gravity="center_horizontal"

Einfach, in der XML-Datei, stellen Sie die Textview Schwerkraft zum Zentrum:

<TextView
    android:gravity="center" />

Sie können wie folgt tun Text zentriert zu bekommen

<TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center" />

Wir können dies erreichen, mit diesen mehr Möglichkeiten: -

  

XML-Methode 01

<TextView  
    android:id="@+id/textView"
    android:layout_height="match_parent"
    android:layout_width="wrap_content" 
    android:gravity="center_vertical|center_horizontal"
    android:text="@strings/text"
/>
  

XML-Methode 02

<TextView  
    android:id="@+id/textView"
    android:layout_height="match_parent"
    android:layout_width="wrap_content" 
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="@strings/text"
/>
  

XML-Methode 03

<TextView  
    android:id="@+id/textView"
    android:layout_height="match_parent"
    android:layout_width="wrap_content" 
    android:gravity="center"
    android:text="@strings/text"
/>
  

XML-Methode 04

<TextView  
    android:id="@+id/textView"
    android:layout_height="match_parent"
    android:layout_width="wrap_content" 
    android:layout_centerInParent="true"
    android:text="@strings/text"
/>
  

Java-Methode 01

textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
  

Java-Methode 02

textview.setGravity(Gravity.CENTER);
  

Java-Methode 03

textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);

Die Textview Höhe und Breite sind Wrap Inhalt dann wird der Text in der Textview immer zentriert sein, dann Zentrum in seinem übergeordneten Layout machen, indem Sie:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Hello.."/>
</RelativeLayout>

Für Linearlayout auch der Code ist gleich:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello.."/>
</LinearLayout>

und pro-grammatisch Elternteil RelativeLayout Java-Code dies zur Laufzeit Gebrauch so etwas in Ihrer Aktivität

TextView textView1 =(TextView)findViewById(R.id.texView1);
RelativeLayout.LayoutParams layoutParams = RelativeLayout.LayoutParams)textView1.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
textView1.setLayoutParams(layoutParams);

Textview Schwerkraft arbeitet wie pro Ihre Eltern Layout.

Linearlayout :

Wenn Sie verwenden Linearlayout, dann werden Sie zwei Schwerkraft finden Attribut android: Schwerkraft & Android: layout_gravity

Android: Dichte: repräsentieren Layout Trank interner Text von Textview während android: layout_gravity. repräsentiert Textview Position in übergeordneter Ansicht

Wenn Sie Text horizontal setzen und vertikal dann unter Code verwenden, Zentrieren dieses

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="300dp"
    android:background="@android:color/background_light"
    android:layout_height="300dp">

    <TextView
        android:layout_width="match_parent"
        android:text="Hello World!"
        android:gravity="center_horizontal"
        android:layout_gravity="center_vertical"
        android:layout_height="wrap_content"
    />
</LinearLayout>

RelativeLayout :

RelativeLayout Verwenden Sie unter Eigenschaft in Textview verwenden können,

Android: Schwerkraft = "center" für Text Zentrum in Textview

.

Android. Gewicht = "center_horizontal" innerer Text, wenn Sie horizontal zentriert werden soll

Android. Gewicht = "center_vertical" innerer Text, wenn Sie vertikal zentriert werden soll

Android: layout_centerInParent = "true", wenn Sie Textview in Mittelposition der übergeordneten Ansicht mögen. Android: layout_centerHorizontal = „true“, wenn Sie möchten Textview in horizontal Zentrum der übergeordneten Ansicht. Android:. layout_centerVertical = "true", wenn Sie Textview in vertikal Zentrum von übergeordneter Ansicht wollen

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="300dp"
    android:background="@android:color/background_light"
    android:layout_height="300dp">

    <TextView
        android:layout_width="match_parent"
        android:text="Hello World!"
        android:gravity="center"
        android:layout_centerInParent="true"
        android:layout_height="wrap_content"
    />
</RelativeLayout>

android:gravity="center_horizontal" für align horizontal Text-Center. android:gravity="center_vertical" für Align vertikal Textzentrum. android:gravity="center" für align text-Center sowohl vertikal als auch horizontal.

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

Versuchen Sie, diese Art und Weise, wird es funktionieren

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center_horizontal|center_vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAlignment="center" />

</LinearLayout>

verwenden Sie den Code unten in xml es für mich gearbeitet, um Ihnen die Orientierung ändern kann es in der Mitte sein wird,

<LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:background="@color/colorPrimaryDark"
        android:gravity="center"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/id_text"
            android:layout_width="wrap_content"
            android:textSize="18sp"
            android:textColor="@android:color/white"
            android:textStyle="normal"
            android:layout_height="wrap_content"
            android:text="centre me"/>
    </LinearLayout>
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top