【Android】AS环境下,在布局中使用android:gravity="left/right"提示使用start/end

1.问题产生

  在AndroidStudio中,在布局文件中使用android:gravity属性设置属性值为left或right时,会有对应的警告提示,提示内容如下:

Use "start" instead of "left" to ensure correct behavior in right-to-left locales less... (Ctrl+F1) 
Using Gravity#LEFT and Gravity#RIGHT can lead to problems when a layout is rendered in locales where 
text flows from right to left. Use Gravity#START and Gravity#END instead. Similarly, in XML gravity 
and layout_gravity attributes, use start rather than left.  For XML attributes such as paddingLeft and 
layout_marginLeft, use paddingStart and layout_marginStart. NOTE: If your minSdkVersion is less than 17, 
you should add both the older left/right attributes as well as the new start/right attributes. On older platforms, 
where RTL is not supported and the start/right attributes are unknown and therefore ignored, you need the older 
left/right attributes. There is a separate lint check which catches that type of error.  
(Note: For Gravity#LEFT and Gravity#START, you can use these constants even when targeting older platforms, because 
the start bitmask is a superset of the left bitmask. Therefore, you can use gravity="start" rather than gravity="left|start".)

2.解决方法

  从上面的警告内容中我们可以得知,当我们的minSdkVersion>=17时,使用start/end来代替left/right;当minSdkVersion<17时,旧的平台不支持RTL,start/end属性是未知的,会被忽略,所以要同时使用start/end和left/right。

3.start/end与left/right有什么区别?

属性说明
leftPush object to the left of its container, not changing its size.
rightPush object to the right of its container, not changing its size.
startPush object to the beginning of its container, not changing its size.
endPush object to the end of its container, not changing its size.

其中left/right是代表一种绝对的对齐,start/end表示基于阅读顺序的对齐。
那么说到阅读顺序又不得不提目前存在的主要阅读方式:
  从左向右(LTR)和从右向左(RTL);当使用left/right的时候,无论是LTR还是RTL,总是左/右对齐的;而使用start/end,在LTR中是左/右对齐,而在RTL中则是右/左对齐。
注:
  left/right属于绝对对齐,而start/end会根据不同国家习惯改变。如阅读顺序是从左到右(LTR)的国家,start在左边,在阅读顺序是从右到左(RTL)的国家,start在右边。

此小节内容参考自http://blog.csdn.net/tiewen/article/details/39925239

  • 7
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值