How to get text direction in Android and change layout dynamically according to the direction?(如何在Android中获取文本方向并根据方向动态更改布局?)
问题描述
我正在研究根据文本方向动态更改 UI 的可行性.尽管 Google 支持 RTL 语言(参考: 类.这个类有 在 if 语句中使用可能会更好.结果和上面一样.
Bidi bidi = new Bidi(userList.get(position).getName(), Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);if(bidi.baseIsLeftToRight())convertView = myInflater.inflate(R.layout.list_add_friends_row, null);别的convertView = myInflater.inflate(R.layout.list_add_friends_row_mirror, null);
I'm doing some research about feasibility of changing UI dynamically based on direction of a text. Although Google is supporting RTL languages (ref: Native RTL support in Android 4.2) but it doesn't cover a situation that application has mix of LTR and RTL contents.
Google's solution is right if we have multilingual app and it has different sources of static data (such as menu items) in different languages. However, I didn't find any other document regard how to change layout dynamically based on direction of text content.
Following image shows current design of my test app. List comes from Facebook (list of my friends) and contains English and Persian names.
My solution is having two layouts (one for LTR and one for RTL) in adapter and assigning each one based on direction of name. So, I wrote following lines of code:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(position % 2 == 0)
convertView = myInflater.inflate(R.layout.list_add_friends_row, null);
else
convertView = myInflater.inflate(R.layout.list_add_friends_row_mirror, null);
holder = new ViewHolder();
holder.llSection = (LinearLayout) convertView.findViewById(R.id.section);
holder.tvUserName = (TextView) convertView.findViewById(R.id.tvUserName);
holder.ivPicture = (ImageView) convertView.findViewById(R.id.ivPicture);
holder.btnAdd = (Button) convertView.findViewById(R.id.btnAdd);
convertView.setTag(holder);
holder.btnAdd.setTag(position);
holder.tvUserName.setText(userList.get(position).getName());
imageDownloader.displayImage(holder.ivPicture, userList.get(position).getPhotoUrl());
...
return convertView;
}
and result is:
I mirrored rows based on if row is even or odd.
Now, my question is how to mirror UI based on name. I think I should change clause condition from if(position % 2 == 0)
to something like if(isNameLTR(FIRST_CHAR_OF_NAME))
. I have no idea how to implement this method.
How can I know a character is LTR ot RTL? (I think Android knows first character of word is RTL or LTR that's why when width of text view is match parent it align text to right if character is RLT and to left if character is LTR).
Any suggestion or comments would be appreciated. Thanks
Solution, was easier that I was thinking :) Thanks to JAVA
There is Bidi class. This class has getBaseLevel() method which returns 0 if your text is left-to-right otherwise 1 (if right-to-left).
So, this is my code:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
Bidi bidi = new Bidi(userList.get(position).getName(), Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
if(bidi.getBaseLevel() == 0)
convertView = myInflater.inflate(R.layout.list_add_friends_row, null);
else
convertView = myInflater.inflate(R.layout.list_add_friends_row_mirror, null);
...
and result is:
=============
Update:
There is another method, baseIsLeftToRight() that might be better to be used in if statement. Result was same as above.
Bidi bidi = new Bidi(userList.get(position).getName(), Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
if(bidi.baseIsLeftToRight())
convertView = myInflater.inflate(R.layout.list_add_friends_row, null);
else
convertView = myInflater.inflate(R.layout.list_add_friends_row_mirror, null);
这篇关于如何在Android中获取文本方向并根据方向动态更改布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在Android中获取文本方向并根据方向动态更改布局?


基础教程推荐
- LocationClient 与 LocationManager 2022-01-01
- :hover 状态不会在 iOS 上结束 2022-01-01
- “让"到底是怎么回事?关键字在 Swift 中的作用? 2022-01-01
- Android文本颜色不会改变颜色 2022-01-01
- 在 iOS 上默认是 char 签名还是 unsigned? 2022-01-01
- 使用 Ryzen 处理器同时运行 WSL2 和 Android Studio 2022-01-01
- 如何使 UINavigationBar 背景透明? 2022-01-01
- 如何使用 YouTube API V3? 2022-01-01
- Android ViewPager:在 ViewPager 中更新屏幕外但缓存的片段 2022-01-01
- 固定小数的Android Money Input 2022-01-01