从右到左的设备在Android的TextView右对齐 [英] Android TextView right alignment on a right-to-left device

查看:587
本文介绍了从右到左的设备在Android的TextView右对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个应用程序,在希伯来语言显示和我是正确对齐的TextView的文本。 虽然开发的Nexus设备上的所有的伟大工程,文本显示,因为它应该使用了android:为TextView的比重=正确的。 当我运行的本地移动运营商设备(以色列),其运行自定义ROM中分别设置重力=右出现,就好像它们是设重力=左所有的TextView小部件在相同的应用程序。如果我去,改变重心向左(你猜对了..),将右对齐。

I am writing an app that displays in the Hebrew language and for that I am right aligning TextView's text. While developing on a Nexus device all works great and texts are displayed as it should, using the android:gravity="right" for TextView. When I am running the same app on a local mobile carrier device (Israel) which is running a custom ROM all TextView widgets that were set with gravity="right" are appearing as if they were set as gravity="left". If I go and change the gravity to left (you guessed it..) it will align right.

我试图改变设备的默认语言环境回美国英语使用code和使用该设备配置本身。没有什么帮助。 我工作的运营商设备是GALAXY SII。

I have tried changing the default locale of the device back to US English using code and using the device configuration itself. Nothing helped. The carrier device I am working on is a Galaxy SII.

在我看来,像有史以来谁的ROM只是改变了对齐值,现在权是指左,左指右。该ROM是来自三星的官方之一。

Seems to me like who ever created the ROM just changed the align values and now right means left and left means right. The ROM is an official one from Samsung.

没有人也遇到过这个问题?任何想法解决方案?

Did anyone experienced this issue? Any ideas for solutions?

我已经尝试过: 1.更改从设置菜单中的设备区域。 2.更改设备区域设置方案。 3.强制根布局在布局文件中左/右layout_gravity /重力

What I've tried already: 1. Changing the device locale from the settings menu. 2. Changing the device locale programmatic. 3. Forcing the root Layout in the layout file to left/right layout_gravity/gravity

唯一的事情目前正在被改变的TextView的重力向左,这意味着它会停止工作,如Nexus的等等...

Only thing currently working is changing the gravity of the TextView to left, which means it stops working on global devices like the Nexus etc...

推荐答案

回答我的问题。

在到处找我发现没有答案。因为ROM是生活在以色列,说希伯来语的人定制的,看来这里面的宽度MATCH_PARENT的TextView对齐翻转。意思是,左对齐指左,右,右手段。 改变这种状况的唯一办法就是使用的TextView与WRAP_CONTENT并对齐的TextView自己的权利里面的父。 因为我已经写的应用程序,所有屏幕的布局,我并不想改变一切。 所以,我所做的是实现了自定义控制,包装一个TextView内的LinearLayout,并公开其从外网的易用性。 这样一来,MATCH_PARENT将影响的LinearLayout而控制将对准包裹的TextView向右照顾。

After looking all over I have found no answer. Because the ROM is custom made for people living in Israel and speaking Hebrew, it seems that the alignment inside a width MATCH_PARENT TextView is flipped. Meaning, Align left means right and right means left. The only way to change that is to use TextView with WRAP_CONTENT and align the TextView itself to the right inside it's parent. Because I've already written the app and all the screens layouts, I did not want to change everything. So what I did is implemented a custom control which wraps a TextView inside a LinearLayout and exposes it for easy use from the out side. This way, the MATCH_PARENT will effect the LinearLayout while the control will take care of aligning the wrapped TextView to the right.

这是怎么:

首先,控制类本身(HebrewTextView.java):

First, the control class itself (HebrewTextView.java):

public class HebrewTextView extends LinearLayout
{
private TextView mTextView;

public HebrewTextView(Context context)
{
    super(context);
    init(null);
}

public HebrewTextView(Context context, AttributeSet attrs)
{
    super(context, attrs);
    init(attrs);
}

public TextView getTextView()
{
    return mTextView;
}

private void init(AttributeSet attrs)
{       
    mTextView = new TextView(getContext());
    LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
    lp.gravity = Gravity.RIGHT; 
    mTextView.setLayoutParams(lp);
    mTextView.setGravity(Gravity.CENTER_VERTICAL);

    addView(mTextView);

    if(attrs!=null)
    {
        TypedArray params = getContext().obtainStyledAttributes(attrs, R.styleable.HebrewTextView);
        mTextView.setText(params.getString(R.styleable.HebrewTextView_android_text));
        mTextView.setTextColor(params.getColor(R.styleable.HebrewTextView_android_textColor, Color.WHITE));
        mTextView.setTextSize(params.getDimension(R.styleable.HebrewTextView_android_textSize, 10));
        mTextView.setSingleLine(params.getBoolean(R.styleable.HebrewTextView_android_singleLine, false));
        mTextView.setLines(params.getInt(R.styleable.HebrewTextView_android_lines, 1));                     
        params.recycle();
    }               
}
}

在值/ attrs.xml控制XML属性。公告称,自定义属性匹配TextView的属性,所以我不会有改变布局文件:

The control XML attributes under values/attrs.xml. Notice that the custom attributes match the TextView attributes so I wont have to change the layout files:

请注意:这些是我所需要的属性,如果你使用的是一些更多的,只是添加到XML和在init阅读()之类的

Note: these are the attributes I needed, if you are using some more, just add to the XML and read it in the init() of the class.

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <declare-styleable name="HebrewTextView">
    <attr name="android:text"/>
    <attr name="android:textColor"/>
    <attr name="android:lines"/>
    <attr name="android:textSize"/>
    <attr name="android:singleLine"/>                                    
</declare-styleable>
</resources>

在这样做了所有我做的是运行在有问题的布局,并与HebrewTextView取代TextView的,如果这是一个由code引用一个TextView,改变铸造。 该方法getTextView()在控制类中定义,因此,在code,改变文字和/或其他的TextView属性添加.getTextView()后缀后,将工作的部分。

After that is done all I had to do is to run over the problematic layouts and replace TextView with HebrewTextView and if it was a TextView that is referenced by the code, change the casting. The method getTextView() was defined in the control class so that sections in the code that changes the text and/or other TextView properties will work after adding the .getTextView() postfix.

希望有所帮助。

这篇关于从右到左的设备在Android的TextView右对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆