align text center with android
Wiem, że to brzmi łatwo. Muszę umieścić tekst w centrum, ale gdy tekst jest zbyt długi, musi przejść poniżej, ale nadal wyrównać w centrum mojego xml.
Oto Mój kod:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/showdescriptioncontenttitle"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_centerHorizontal="true"
>
<TextView
android:id="@+id/showdescriptiontitle"
android:text="Title"
android:textSize="35dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
Wstawiam paddingTop i Bottom, ponieważ potrzebuję trochę miejsca. PS: mój kod jest większy, jest w relatywnie
6 answers
Ustaw również parametr android:gravity
w TextView
na center
.
Do testowania efektów różnych parametrów układu zalecam użycie innego koloru tła dla każdego elementu, aby zobaczyć, jak zmienia się układ z parametrami takimi jak gravity, layout_gravity lub innymi.
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-09-06 21:29:42
Użyj tej drogi
txt.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
2013-11-02 15:39:05
Użyj tego sposobu w xml
<TextView
android:id="@+id/myText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Time is precious, so love now."
android:gravity="center"
android:textSize="30dp"
android:textColor="#fff"
/>
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-09 06:07:33
Możesz użyć:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/showdescriptioncontenttitle"
android:paddingTop="10dp"
android:paddingBottom="10dp">
<TextView
android:id="@+id/textview1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Your text"
android:typeface="serif" />
</LinearLayout>
Układ musi być względny, aby właściwość" center " została użyta.
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-05-23 21:14:29
Dodanie android:gravity="center"
w widoku tekstowym wystarczy (układ nadrzędny to Relative/Linear
)!
Należy również unikać używania dp dla rozmiaru czcionki. Zamiast tego użyj sp.
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-24 08:43:20
Dodaj layout_gravity i grawitację z wartością środka na TextView
<TextView
android:text="welcome text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
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
2016-10-30 07:22:41