问题描述
我在做有关更改用户界面动态地根据文本方向的可行性,一些研究。虽然谷歌正在支持RTL语言(参考:本地RTL支持安卓4.2 ),但不包括这种情况的应用程序具有LTR和RTL内容组合。
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.
下图显示了我的测试应用程序当前的设计。列表来自Facebook的(我的朋友名单),包含英语和波斯语名字。
Following image shows current design of my test app. List comes from Facebook (list of my friends) and contains English and Persian names.
我的解决办法是有两种布局(一为LTR,一个用于RTL)适配器和分配每个人的基础上名的方向。所以,我写了下面的code线:
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;
}
和结果是:
我反映的基础上,如果行是奇数还是偶数行。现在,我的问题是如何根据名字来反映UI。我想我应该改变子句条件的如果(位置%2 == 0)
来像如果(isNameLTR(FIRST_CHAR_OF_NAME))
。我不知道如何实现此方法。
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.
我怎么能知道一个字符LTR加时赛RTL?(我认为Android知道单词的第一个字符是RTL或LTR这就是为什么当宽的文本视图是匹配父母它对齐文本的权利,如果字符是锐力和左如果字符是LTR)。
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).
任何建议或意见,将AP preciated。谢谢
Any suggestion or comments would be appreciated. Thanks
推荐答案
解决方案,是比较容易,我想:)感谢JAVA
Solution, was easier that I was thinking :) Thanks to JAVA
有比迪烟类。这个类有getBaseLevel()方法返回0,如果你的文本是左到右,否则1(如果从右到左)。
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).
所以,这是我的code:
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);
...
和结果是:
=============
=============
更新
还有另一种方法,baseIsLeftToRight()这可能是更好地用在if语句。结果与上述相同。
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和改变布局根据方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!