Jak wyśrodkować tekst w poziomie i w pionie w widoku tekstowym?
Jak wyśrodkować tekst w poziomie i pionie w TextView
, aby pojawił się dokładnie w środku TextView
w Android
?
30 answers
Zakładam, że używasz układu XML.
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/**yourtextstring**"
/>
Możesz również użyć grawitacji center_vertical
lub center_horizontal
w zależności od potrzeb.
I jak skomentował @stealthcopter
w języku java: .setGravity(Gravity.CENTER);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-07-10 16:11:32
android:gravity="center"
This will do the trick
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-03-22 14:27:07
Możesz również ustawić go dynamicznie za pomocą:
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-11-21 12:25:56
android:layout_centerInParent="true"
To działa, gdy jest używane z RelativeLayout, gdzie wysokość i szerokość układu są ustawione na wrap_content.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-11-11 03:33:26
Możesz również użyć kombinacji:
android:gravity="left|center"
Wtedy, jeśli szerokość widoku tekstu jest większa niż "fill_parent", tekst będzie nadal wyrównany do lewej (Nie wyśrodkowany, jak w przypadku grawitacji ustawionej tylko na"center").
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-07-26 08:36:05
Zastosuj grawitację:
TextView txtView = (TextView) findViewById(R.id.txtView);
txtView.setGravity(Gravity.CENTER_HORIZONTAL);
Dla pionu:
txtView.setGravity(Gravity.CENTER_VERTICAL);
W XML:
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="@string/Hello_World"
/>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-02-17 11:10:19
Jeśli używasz TableLayout upewnij się, aby ustawić grawitację TableRows do środka, zbyt. W przeciwnym razie to nie zadziała. Przynajmniej nie zadziałało ze mną, dopóki nie ustawiłem ciężaru stołu na środku.
Na przykład tak:
<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>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-05-13 04:49:09
Musisz ustawić TextView Gravity (Center Horizontal & Center Vertical) w następujący sposób:
android:layout_centerHorizontal="true"
I
android:layout_centerVertical="true"
I dynamicznie używając:
textview.setGravity(Gravity.CENTER);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-05-13 04:49:52
Można to zrobić na dwa sposoby.
Pierwszy w kodzie XML. Musisz zwrócić uwagę na atrybut Gravity
. Można również znaleźć ten atrybut w edytorze graficznym; może to być łatwiejsze niż edytor XML.
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical|center_horizontal"
android:text="Your Text"
/>
Dla Twojego konkretnego scenariusza, wartości grawitacji będą następujące:
center_vertical|center_horizontal
W edytorze graficznym znajdziesz wszystkie możliwe wartości, nawet zobaczyć ich wyniki.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-09-27 13:01:40
Moim zdaniem,
android:gravity="center"
Jest lepszy niż,
android:layout_centerInParent="true"
Który jest lepszy niż,
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
Przynajmniej do formatowania tekstu.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-01-12 04:40:21
Dla Układu Liniowego: W XML użyj czegoś takiego
<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"
/>
Aby to zrobić w czasie wykonywania użyj czegoś takiego w swojej aktywności
TextView textView1 =(TextView)findViewById(R.id.texView1);
textView1.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
Dla układu względnego: w XML użyj czegoś takiego
<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"
/>
Aby to zrobić w czasie wykonywania użyj czegoś takiego w swojej aktywności
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);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-01-19 06:33:00
Użyj w pliku XML.
Plik układu
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="@string/stringtext"/>
Lub:
Użyj tego wewnątrz klasy Java
TextView textView =(TextView)findViewById(R.id.texviewid);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-05-13 04:57:25
Użyj tego dla względnego układu
android:layout_centerInParent="true"
I dla innych układów
android:gravity="center"
Podczas korzystania z gravity works dla TextView, istnieje alternatywna metoda zaimplementowana w API poziom 17-
textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
Nie znam różnicy, ale to też działa. Jednak tylko dla poziomu API 17 lub wyższego.Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-08-24 20:50:50
W RelativeLayout
, będzie miło z nim.
I inne {[2] } i wszystko, co możesz dodać.
Poniższe działa mi dobrze.
<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>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-04-20 15:43:03
Jeśli TextView's
wysokość i szerokość są wrap content
, to tekst wewnątrz TextView
zawsze będzie wyśrodkowany. Ale jeśli TextView's
szerokość wynosi match_parent
, a wysokość to match_parent
lub wrap_content
to musisz napisać poniższy kod:
Dla RelativeLayout:
<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>
Dla LinearLayout:
<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>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-12 15:15:59
Najprostszy sposób (o dziwo wspominany tylko w komentarzach, dlatego zamieszczam jako odpowiedź) to:
textview.setGravity(Gravity.CENTER)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-12-15 12:38:02
Możesz po prostu ustawić gravity
swojego widoku tekstowego na CENTER
.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-09-27 13:02:15
Jeśli próbujesz wyśrodkować tekst na TableRow w TableLayout, oto jak to osiągnąłem:
<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>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-07-15 02:14:08
Oto moja odpowiedź, której użyłem w mojej aplikacji. Wyświetla tekst na środku ekranu.
<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" />
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-05-13 04:56:27
Jeśli używasz układu względnego:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/stringname"
android:layout_centerInParent="true"/>
Jeśli używasz LinearLayout]}
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/stringname"
android:layout_gravity="center"/>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-11-15 06:33:26
Jak wiele odpowiedzi sugeruje powyżej działa dobrze.
android:gravity="center"
Jeśli chcesz wyśrodkować go tylko pionowo:
android:gravity="center_vertical"
Lub po prostu poziomo:
android:gravity="center_horizontal"
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-07-04 20:09:04
Wystarczy w pliku XML ustawić textview na środek:
<TextView
android:gravity="center" />
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-05-13 04:54:11
Wysokość i szerokość widoku tekstowego są zawijane, następnie tekst wewnątrz widoku tekstowego jest zawsze wyśrodkowany, a następnie wyśrodkowany w jego nadrzędnym układzie za pomocą:
<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>
Dla LinearLayout również kod jest taki sam:
<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>
A rodzicem pro-gramatycznym jest relatywnie prosty kod Javy, który w czasie wykonywania użyj czegoś takiego w swojej aktywności
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);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-07 13:42:51
Możesz zrobić tak, aby wyśrodkować tekst
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" />
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-06-30 09:27:32
Możemy to osiągnąć na wiele sposobów: -
Metoda XML 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"
/>
Metoda XML 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"
/>
Metoda XML 03
<TextView
android:id="@+id/textView"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:gravity="center"
android:text="@strings/text"
/>
Metoda XML 04
<TextView
android:id="@+id/textView"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:text="@strings/text"
/>
Metoda Java 01
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
Metoda Java 02
textview.setGravity(Gravity.CENTER);
Metoda Java 03
textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-06-14 10:32:20
android:gravity="center_horizontal"
wyrównaj tekst w poziomie.
android:gravity="center_vertical"
For align text Center pionowo.
android:gravity="center"
Dla wyrównania tekstu w pionie i poziomie.
<TextView
android:layout_width="match_parent"
android:gravity="center_horizontal|center_vertical"
android:layout_height="match_parent" />
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-05-20 10:18:11
Spróbuj w ten sposób, to zadziała
<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>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-07-28 07:03:10
Użyj poniższego kodu w xml to działało dla mnie można zmienić orientację to będzie w centrum
<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>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-12-07 10:26:56
TextView gravity działa zgodnie z układem nadrzędnym.
LinearLayout :
Jeśli użyjesz LinearLayout to znajdziesz dwa atrybuty grawitacji android: gravity & android:layout_gravity
Android: gravity: reprezentują układ wewnętrzny tekstu TextView podczas android: layout_gravity: reprezentuje pozycję TextView w widoku nadrzędnym.
Jeśli chcesz ustawić tekst poziomo i pionowo na środku, użyj poniższego kodu to
<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 :
Używając RelativeLayout możesz użyć poniższej właściwości w TextView
Android:gravity="center" dla Text center w TextView.
Android: gravity= "center_horizontal" wewnętrzny tekst, jeśli chcesz wyśrodkować poziomo.
Android: gravity= "center_vertical" wewnętrzny tekst, jeśli chcesz wyśrodkować pionowo.
Android: layout_centerInParent= "true" jeśli chcesz TextView w pozycji środkowej rodzica widok. android: layout_centerHorizontal = "true" jeśli chcesz TextView w poziomym centrum widoku nadrzędnego. android: layout_centerVertical = "true" jeśli chcesz TextView w pionowym centrum widoku nadrzędnego.
<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>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-10-27 13:08:01