简体   繁体   English

从右到左设备上的Android TextView右对齐

[英]Android TextView right alignment on a right-to-left device

I am writing an app that displays in the Hebrew language and for that I am right aligning TextView's text. 我正在编写一个以希伯来语显示的应用程序,因此我正确地对齐TextView的文本。 While developing on a Nexus device all works great and texts are displayed as it should, using the android:gravity="right" for TextView. 在Nexus设备上进行开发时,所有工作都很棒并且文本显示应该使用,对于TextView使用android:gravity =“right”。 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". 当我在运行自定义ROM的本地移动运营商设备(以色列)上运行相同的应用程序时,所有使用gravity =“right”设置的TextView小部件看起来好像被设置为gravity =“left”。 If I go and change the gravity to left (you guessed it..) it will align right. 如果我去将重力改为左(你猜对了......)它会对齐。

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. 我正在研究的运营商设备是Galaxy SII。

Seems to me like who ever created the ROM just changed the align values and now right means left and left means right. 在我看来,曾经创建ROM的人只是改变了对齐值,现在正确意味着左右意味着正确。 The ROM is an official one from Samsung. ROM是三星的官方ROM。

Did anyone experienced this issue? 有没有人遇到过这个问题? Any ideas for solutions? 任何解决方案的想法?

What I've tried already: 1. Changing the device locale from the settings menu. 我已经尝试过的方法:1。从设置菜单中更改设备区域设置。 2. Changing the device locale programmatic. 2.以编程方式更改设备区域设置。 3. Forcing the root Layout in the layout file to left/right layout_gravity/gravity 3.强制布局文件中的根布局为left / right layout_gravity / gravity

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... 目前唯一有效的方法是将TextView的重力改为左侧,这意味着它将停止在Nexus等全球设备上工作......

Answering my own question. 回答我自己的问题。

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. 因为ROM是为居住在以色列并讲希伯来语的人定制的,所以似乎翻转了宽度MATCH_PARENT TextView内的对齐方式。 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. 更改它的唯一方法是将TextView与WRAP_CONTENT一起使用,并将TextView本身对齐在其父级内部的右侧。 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. 所以我所做的是实现了一个自定义控件,它将TextView包装在LinearLayout中并公开它以便从外面使用。 This way, the MATCH_PARENT will effect the LinearLayout while the control will take care of aligning the wrapped TextView to the right. 这样,MATCH_PARENT将影响LinearLayout,而控件将负责将包装的TextView对齐到右侧。

And this is how: 这是如何:

First, the control class itself (HebrewTextView.java): 首先,控件类本身(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();
    }               
}
}

The control XML attributes under values/attrs.xml. 值/ attrs.xml下的控件XML属性。 Notice that the custom attributes match the TextView attributes so I wont have to change the layout files: 请注意,自定义属性与TextView属性匹配,因此我不必更改布局文件:

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并在类的init()中读取它。

<?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>

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. 完成之后我所要做的就是遍历有问题的布局并用HebrewTextView替换TextView,如果它是代码引用的TextView,则更改转换。 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. 方法getTextView()是在控件类中定义的,因此在添加.getTextView()后缀后,代码中更改文本和/或其他TextView属性的部分将起作用。

Hope that helps. 希望有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM