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

来源:互联网 发布:设置数据有效性男女 编辑:程序博客网 时间:2024/05/21 06:34

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".)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

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

0 0
原创粉丝点击