I've made an XML which include a TextView
having a textAlignement:center
in the following code :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<Button
android:layout_width="0px"
android:layout_weight="0.24"
android:text="gestion"
android:layout_height="wrap_content"
android:id="@+id/GoGestion"/>
<View
android:layout_width="0px"
android:layout_weight="0.01"
android:layout_height="match_parent"></View>
<TextView
android:layout_width="0px"
android:layout_weight="0.5"
android:layout_height="wrap_content"
android:text="Creation Sav"
android:textSize="40dp"
android:layout_gravity="center"
android:textAlignment="center"/>
<View
android:layout_width="0px"
android:layout_weight="0.01"
android:layout_height="match_parent"></View>
<Button
android:layout_width="0px"
android:layout_weight="0.24"
android:text="Archive"
android:layout_height="wrap_content"
android:id="@+id/GoArchive"/>
</LinearLayout>
And the fact is that My textView isn't centered on a tablet having 4.1.2 version (jellybean) but is on the other tablet 4.4.2 (kitkat).
My app compiles with minSdkVersion 15
so jellybean is more recent. Nowhere in the doc is specified that Jellybean does not run textAlignement.
Why is it not working on jelylbean and how can I fix this?
You can set this in your TextView
android:gravity="center"
Place the object in the center of its container in both the vertical and horizontal axis, not changing its size.
Please check Gravity and layout_gravity on Android . Hope this helps .
<TextView
android:layout_width="0px"
android:layout_weight="0.5"
android:layout_height="wrap_content"
android:text="Creation Sav"
android:textSize="40dp"
android:layout_gravity="center"
android:textAlignment="center"/>
change to
<TextView
android:layout_width="0px"
android:layout_weight="0.5"
android:layout_height="wrap_content"
android:text="Creation Sav"
android:textSize="40dp"
android:gravity="center"
android:textAlignment="center"/>
android:gravity
sets the gravity of the contents wiz.subviews
insidecontainer
.
android:layout_gravity
sets the gravity of theView/Layout
relative to its parentLayout/Container
. Everything withlayout_
defines something that effects the elements outside.
Put this code
<?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="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<Button
android:id="@+id/GoGestion"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="0.24"
android:text="gestion" />
<View
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="0.01"></View>
<TextView
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.5"
android:text="Creation Sav"
android:gravity="center"
android:textAlignment="center"
android:textSize="40dp" />
<View
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="0.01"></View>
<Button
android:id="@+id/GoArchive"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="0.24"
android:text="Archive" />
</LinearLayout>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.