How to right align PreferencesActivity in android?

2019-02-06 09:24发布

I have PreferencesActivity which I need it to be right aligned because I want to use Arabic language, I tried to use android:layout_gravity="right" for PreferenceScreen but it didn't work.

This is my XML:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" android:layout_gravity="right">
        <PreferenceCategory android:title="General Settings">
                <CheckBoxPreference
                        android:title="Full Screen"
                        android:defaultValue="false"
                        android:summary="Always view as Full Screen"
                        android:key="fullScreenPref" />
        <Preference
                android:title="Report Bugs"
                android:summary="Notify us for any Bugs or Errors"
                android:key="bugs"/>
        <Preference
                android:title="About"
                android:summary="Version 1.0.0"
                android:key="about"/>
        </PreferenceCategory>
</PreferenceScreen>

This is how I use the XML inside PreferencesActivity:

addPreferencesFromResource(R.layout.preferences);

13条回答
成全新的幸福
2楼-- · 2019-02-06 09:57

Arabic text has posed a problem for many a user which means that similar, if not the same, questions have been answered several times.

I'll just leave these here

Android Arabic text aligment and

How to support Arabic text in Android?

查看更多
你好瞎i
3楼-- · 2019-02-06 10:00

In your manifest file under application tag add this line,

android:supportsRtl="true"

查看更多
对你真心纯属浪费
4楼-- · 2019-02-06 10:03

After checking for a few solutions i came upon something that might help. It's not elegant and works only on 4 and up but it's better than nothing...

add this code to onResume()

    Locale locale = new Locale("iw");// Hebrew in this case
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config,
          getBaseContext().getResources().getDisplayMetrics());

Of course you can choose to change the locale in your preference activities onResume() method only in your MainActivity's onResume() change locale back to your preferred (or user defined) locale.

Hope this helps!

查看更多
老娘就宠你
5楼-- · 2019-02-06 10:03
public class RtlEditTextPreference extends EditTextPreference {
public RtlEditTextPreference(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public RtlEditTextPreference(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

public RtlEditTextPreference(Context context) {
    super(context);
}

@Override
protected View onCreateView(ViewGroup parent) {
    View view = super.onCreateView(parent);

    RelativeLayout relativeLayout = (RelativeLayout)(((LinearLayout)view).getChildAt(1));
    relativeLayout.setGravity(Gravity.RIGHT);

    TextView title = (TextView) relativeLayout.getChildAt(0);
    TextView summary = (TextView) relativeLayout.getChildAt(1);

    RelativeLayout.LayoutParams titleLayoutParams = (RelativeLayout.LayoutParams)title.getLayoutParams();
    titleLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    title.setLayoutParams(titleLayoutParams);

    RelativeLayout.LayoutParams summaryLayoutParams = (RelativeLayout.LayoutParams)summary.getLayoutParams();
    summaryLayoutParams.addRule(RelativeLayout.ALIGN_RIGHT, title.getId());
    summaryLayoutParams.addRule(RelativeLayout.ALIGN_LEFT, -1); // Override default left alignment to the title
    summary.setLayoutParams(summaryLayoutParams);

    return view;
}

}

查看更多
该账号已被封号
6楼-- · 2019-02-06 10:05
public class RtlEditTextPreference extends EditTextPreference {

@Override
protected View onCreateView(ViewGroup parent) {
    View view = super.onCreateView(parent);

    RelativeLayout relativeLayout = (RelativeLayout)(((LinearLayout)view).getChildAt(1));
    relativeLayout.setGravity(Gravity.RIGHT);

    TextView title = (TextView) relativeLayout.getChildAt(0);
    TextView summary = (TextView) relativeLayout.getChildAt(1);

    RelativeLayout.LayoutParams titleLayoutParams = (RelativeLayout.LayoutParams)title.getLayoutParams();
    titleLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    title.setLayoutParams(titleLayoutParams);

    RelativeLayout.LayoutParams summaryLayoutParams = (RelativeLayout.LayoutParams)summary.getLayoutParams();
    summaryLayoutParams.addRule(RelativeLayout.ALIGN_RIGHT, title.getId());
    summaryLayoutParams.addRule(RelativeLayout.ALIGN_LEFT, -1); // Override default left alignment to the title
    summary.setLayoutParams(summaryLayoutParams);

    return view;
}

}

查看更多
手持菜刀,她持情操
7楼-- · 2019-02-06 10:06

Unfortunately the base Preference components (PreferenceScreen, Preference, etc.) are implemented in a fairly non-configurable way. They are designed to work in a specific way and are not terribly customizable.

In order to do what you want, you will probably have to implement your own version of PreferenceScreen (or possible just the Preference types you are using such as CheckBoxPreference). The way it is designed, when it inflates XML layouts it assumes a lot of things about them and handles it for you (text size, padding, layout, wrapping, etc). This is great if you want it to look like the default, but not so great if you need to tweak these configurations.

(note: if you don't want to implement preferencescreen you could just use a listview and treat it as a preference page. However, either way you basically have to implement your own version of preferences.)

(note2: based on other questions I've seen about arabic text, it's POSSIBLE android is smart enough to try to right align it by default. However, I would be surprised if this was the case. Still, it's worth a try.)

Sorry I don't have a better answer for you.

查看更多
登录 后发表回答