Android TextView right alignment on a right-to-left device

好久不见. 提交于 2019-12-04 12:02:52

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

And this is how:

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();
    }               
}
}

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:

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>

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.

Hope that helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!