Hoe centreer ik tekst horizontaal en verticaal in een TextView?

Hoe centreer ik de tekst horizontaal en verticaal in een TextView's, zodat deze precies in het midden van de TextView'sin Androidverschijnt ?


Antwoord 1, autoriteit 100%

Ik neem aan dat je een XML-layout gebruikt.

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

Je kunt ook zwaartekracht center_verticalof center_horizontalgebruiken, afhankelijk van je behoefte.

en zoals @stealthcopter opmerkte
in java: .setGravity(Gravity.CENTER);


Antwoord 2, autoriteit 14%

android:gravity="center" 

Dit zal het lukken


Antwoord 3, autoriteit 9%

Je kunt het ook dynamisch instellen met:

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

Antwoord 4, autoriteit 4%

android:layout_centerInParent="true"

Dit werkt bij gebruik met een RelativeLayout waarbij de hoogte & breedte zijn ingesteld op wrap_content.


Antwoord 5, autoriteit 3%

Je kunt ook de combinatie gebruiken:

android:gravity="left|center"

Als de breedte van de tekstweergave meer is dan ‘fill_parent’, wordt de tekst nog steeds links uitgelijnd (niet gecentreerd zoals met zwaartekracht alleen ingesteld op ‘center’).


Antwoord 6, autoriteit 2%

Zwaartekracht toepassen:

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

Voor verticaal:

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

Antwoord 7

Er zijn twee manieren om dit te doen.

De eerste in de XML-code. Je moet letten op het kenmerk Gravity. U kunt dit attribuut ook vinden in de Grafische Editor; het is misschien makkelijker dan de XML EDITOR.

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

Voor uw specifieke scenario zijn de zwaartekrachtwaarden:

center_vertical|center_horizontal

In de grafische editor vindt u alle mogelijke waarden, zelfs hun resultaten.


Antwoord 8

Als je TableLayout gebruikt, zorg er dan voor dat de zwaartekracht van de TableRows ook in het midden staat.
Anders zal het niet werken. Bij mij werkte het tenminste niet totdat ik de zwaartekracht van de TableRow in het midden zette.

Bijvoorbeeld als volgt:

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

Antwoord 9

U moet TextView Gravity(Center Horizontal & Center Vertical) als volgt instellen:

android:layout_centerHorizontal="true"   

en

android:layout_centerVertical="true"

En dynamisch met:

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

Antwoord 10

Naar mijn mening,

android:gravity="center" 

is beter dan,

android:layout_centerInParent="true"

wat beter is dan,

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

tenminste voor het opmaken van tekst.


Antwoord 11

Voor lineaire lay-out:
Gebruik in XML zoiets als dit

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

Om dit tijdens runtime te doen, gebruik je zoiets in je activiteit

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

Voor relatieve lay-out: gebruik in XML zoiets als dit

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

Om dit tijdens runtime te doen, gebruik je zoiets in je activiteit

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);

Antwoord 12

Gebruik in het XML-bestand.

Indelingsbestand

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

of:

Gebruik dit binnen de Java-klasse

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

Antwoord 13

Gebruik dit voor relatieve lay-out

android:layout_centerInParent="true"

en voor andere lay-out

android:gravity="center" 

Antwoord 14

Als de hoogte en breedte van de TextView'swrap contentzijn, dan wordt de tekst in de TextView'saltijd gecentreerd. Maar als de breedte van TextView'smatch_parentis en de hoogte match_parentof wrap_contentdan moet je de onderstaande code schrijven:

Voor Relatieve indeling:

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

Voor Lineaire Lay-out:

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

Antwoord 15

Terwijl het gebruik van zwaartekracht werkt voor TextView, is er een alternatieve methode geïmplementeerd in API-niveau 17 –

textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);

Ken het verschil niet, maar het werkt ook. Echter alleen voor API-niveau 17 of hoger.


Antwoord 16

In RelativeLayoutzal het leuk zijn.

En nog een Buttonen al het andere dat je kunt toevoegen.

Het volgende werkt goed voor mij.

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

Antwoord 17

De eenvoudigste manier (die verrassend genoeg alleen in reacties wordt genoemd, vandaar dat ik als antwoord post) is:

textview.setGravity(Gravity.CENTER)

Antwoord 18

Je kunt de gravityvan je tekstweergave gewoon instellen op CENTER.


Antwoord 19

Als je tekst probeert te centreren op een TableRow in een TableLayout, dan heb ik dit als volgt bereikt:

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

Antwoord 20

Gebruik android:textAlignment="center"

      <TextView
        android:text="HOW WAS\nYOUR\nDAY?"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:id="@+id/textView5"
         />

Antwoord 21

Hier is mijn antwoord dat ik in mijn app had gebruikt. Het toont tekst in het midden van het scherm.

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

Antwoord 22

Als u relatieve lay-out gebruikt:

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

Als u LinearLayout gebruikt

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

Antwoord 23

Zoals veel antwoorden hierboven suggereren, werkt het prima.

android:gravity="center" 

Als je het alleen verticaal wilt centreren:

android:gravity="center_vertical"

of gewoon horizontaal:

android:gravity="center_horizontal"

Antwoord 24

Eigenlijk kunnen we het beter doen door fontPadding uit te sluiten.

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

Antwoord 25

Stel in uw XML-bestand eenvoudig de zwaartekracht van de tekstweergave in op het midden:

<TextView
    android:gravity="center" />

Antwoord 26

Je kunt dit op deze manier doen om tekst gecentreerd te krijgen

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

Antwoord 27

De hoogte en breedte van TextView zijn terugloopinhoud, de tekst in de tekstweergave wordt altijd gecentreerd en centreer vervolgens in de bovenliggende lay-out met behulp van:

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

Voor LinearLayout is de code ook hetzelfde :

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

en pro-grammatisch ouder is RelativeLayout Java-code dit tijdens runtime gebruik zoiets als dit in uw activiteit

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);

Antwoord 28

We kunnen dit op meerdere manieren bereiken:-

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);

Antwoord 29

TextView-zwaartekracht werkt volgens uw bovenliggende lay-out.

Lineaire indeling:

Als u LinearLayout gebruikt, vindt u twee zwaartekrachtattributen
androïde:zwaartekracht & android:layout_gravity

android:gravity : representeer de lay-out van de interne tekst van TextView while
android:layout_gravity: vertegenwoordigt TextView-positie in bovenliggende weergave.

voer hier de afbeeldingsbeschrijving in

Als u tekst horizontaal wilt plaatsen & verticaal centreren gebruik dan onderstaande code dit

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

Relatieve indeling:

Met RelativeLayout kunt u onderstaande eigenschap in TextView gebruiken

android:gravity=”center” voor tekstcentrum in TextView.

android:gravity=”center_horizontal” binnentekst als je horizontaal gecentreerd wilt.

android:gravity=”center_vertical” binnentekst als je verticaal gecentreerd wilt hebben.

android:layout_centerInParent=”true” als u TextView in het midden van de bovenliggende weergave wilt hebben.
android:layout_centerHorizontal=”true” als u TextView horizontaal in het midden van de bovenliggende weergave wilt.
android:layout_centerVertical=”true” als u TextView verticaal in het midden van de bovenliggende weergave wilt hebben.

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

Antwoord 30

Probeer het op deze manier, het zal werken

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center">
        <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAlignment="center"/>
</LinearLayout>

LEAVE A REPLY

Please enter your comment!
Please enter your name here

four × three =

Other episodes