【问题标题】:Android TextView right alignment on a right-to-left device从右到左设备上的 Android TextView 右对齐
【发布时间】:2013-03-16 11:34:43
【问题描述】:

我正在编写一个以希伯来语显示的应用程序,为此我正在右对齐 TextView 的文本。 在 Nexus 设备上开发时,所有工作都很好,文本显示正常,使用 android:gravity="right" 作为 TextView。 当我在运行自定义 ROM 的本地移动运营商设备(以色列)上运行相同的应用程序时,所有设置为重力 =“右”的 TextView 小部件看起来好像它们被设置为重力 =“左”。如果我去将重力向左改变(你猜对了......)它会向右对齐。

我尝试使用代码并使用设备配置本身将设备的默认语言环境更改回美国英语。没有任何帮助。 我正在使用的运营商设备是 Galaxy SII。

在我看来,创建 ROM 的人只是更改了对齐值,现在右表示左,左表示右。 ROM 是三星官方的。

有人遇到过这个问题吗?有什么解决方案的想法吗?

我已经尝试过的: 1. 从设置菜单更改设备区域设置。 2. 以编程方式更改设备区域设置。 3. 强制布局文件中的根Layout为left/right layout_gravity/gravity

目前唯一有效的是将 TextView 的重力向左更改,这意味着它停止在 Nexus 等全球设备上工作......

【问题讨论】:

    标签: java android android-layout android-widget right-to-left


    【解决方案1】:

    回答我自己的问题。

    翻遍后,我没有找到答案。因为 ROM 是为居住在以色列并讲希伯来语的人定制的,所以宽度 MATCH_PARENT TextView 内的对齐似乎被翻转了。意思是,左对齐意味着右,右意味着左。 改变这一点的唯一方法是将 TextView 与 WRAP_CONTENT 一起使用,并将 TextView 本身与它的父级内的右侧对齐。 因为我已经编写了应用程序和所有屏幕布局,所以我不想更改所有内容。 所以我所做的是实现了一个自定义控件,它将一个 TextView 包装在一个 LinearLayout 中并公开它以便从外部使用。 这样,MATCH_PARENT 将影响 LinearLayout,而控件将负责将包装的 TextView 向右对齐。

    这就是方法:

    首先,控件类本身(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();
        }               
    }
    }
    

    values/attrs.xml 下的控件 XML 属性。请注意,自定义属性与 TextView 属性匹配,因此我不必更改布局文件:

    注意:这些是我需要的属性,如果你使用更多,只需添加到 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>
    

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

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2018-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-15
      • 1970-01-01
      相关资源
      最近更新 更多