android - Gravity of two side-by-side TextViews -


i'm trying have 2 textviews side-by-side, , want 1 touching right-side of screen , other, left-side. don't want define widths using numbers because screens of different sizes behave differently. i'm trying use layout_gravity, not working reason.

<linearlayout     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:orientation="horizontal"     >  <textview     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:textsize="16dp"     android:layout_gravity="left"     android:text="rrr"     android:textcolor="@color/secondtextcolor"     />  <textview     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_gravity="right"     android:textsize="16dp"     android:text="sss"     android:textcolor="@color/secondtextcolor" />  </linearlayout> 

can tell me why? thanks!

you can create 1 linearlayout each textview follows :

<linearlayout     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:orientation="horizontal"     android:gravity="start">     <textview         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:textsize="16dp"         android:text="rrr"         android:textcolor="#f2f2"         />     <linearlayout         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:gravity="end">         <textview             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:textsize="16dp"             android:text="sss"             android:textcolor="#f3f3" />     </linearlayout> </linearlayout> 

and important thing in first linearlayout put android:gravity="start" , in second 1 android:gravity="end", work :)

use end instead of right ensure correct behavior in right-to-left locales.

why "end" better "right"?

using gravity#left , gravity#right can lead problems when layout rendered in locales text flows right left. use gravity#start , gravity#end instead. similarly, in xml gravity , layout_gravity attributes, use start rather left. xml attributes such paddingleft , layout_marginleft, use paddingstart , layout_marginstart. note: if minsdkversion less 17, should add both older left/right attributes new start/right attributes. on older platforms, rtl not supported , start/right attributes unknown , therefore ignored, need older left/right attributes. there separate lint check catches type of error.

(note: gravity#left , gravity#start, can use these constants when targeting older platforms, because start bitmask superset of left bitmask. therefore, can use gravity="start" rather gravity="left|start".)


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -