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条回答
Luminary・发光体
2楼-- · 2019-02-06 09:46
create a layout xml file called preference_checkbox.xml paste this (change the colour to what you want, only the gravity and the width set to fill parent are important) :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical" android:paddingRight="?android:attr/scrollbarSize">

<RelativeLayout android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:layout_marginLeft="15dip"
    android:layout_marginRight="6dip" android:layout_marginTop="6dip"
    android:layout_marginBottom="6dip" android:layout_weight="1">

    <TextView android:id="@+android:id/title"
        android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="right"
        android:singleLine="true" android:textAppearance="?android:attr/textAppearanceMedium"
        android:ellipsize="marquee" android:fadingEdge="horizontal"
        android:textColor=#000000 />

    <TextView android:id="@+android:id/summary"
        android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="right"
        android:layout_below="@android:id/title" android:layout_alignLeft="@android:id/title"
        android:textAppearance="?android:attr/textAppearanceSmall" android:textColor=#000000
        android:maxLines="4" />

</RelativeLayout>

<!-- Preference should place its actual preference widget here. -->
<LinearLayout android:id="@+android:id/widget_frame"
    android:layout_width="wrap_content" android:layout_height="match_parent"
    android:gravity="center_vertical" android:orientation="vertical" />

</LinearLayout>

and then in the preferences xml file for each row in the settings list that you want to right align add this: android:layout="@layout/preference_checkbox"

thats it!
got the solution from : http://stackoverflow.com/questions/7094584/checkboxpreference-with-own-layout
the question there was about check box with image but it's the same technique.
Good Luck.
查看更多
Animai°情兽
3楼-- · 2019-02-06 09:46

same as my problem and this is the solution:

inside your PreferenceFragment:

@Override
    public View onCreateView(LayoutInflater paramLayoutInflater,
            ViewGroup paramViewGroup, Bundle paramBundle) {
        getActivity().setTitle(R.string.fragment_title_settings);
        View v = super.onCreateView(paramLayoutInflater, paramViewGroup,
                paramBundle);
        rtlView(v);
        return v;
    }
    public void rtlView(View v){
        if(v instanceof ListView){
            rtllater((ListView) v);
        }
        else
        if(v instanceof ViewGroup){
            if(v instanceof RelativeLayout){
                ((RelativeLayout)v).setGravity(Gravity.RIGHT);
            }
            else
            if(v instanceof LinearLayout){
                ((LinearLayout)v).setGravity(Gravity.RIGHT);
            }
            for (int i=0;i<((ViewGroup)v).getChildCount();i++){
                rtlView(((ViewGroup)v).getChildAt(i));
            }
        }
        else
            if(v instanceof TextView){
                v.getLayoutParams().width=v.getLayoutParams().MATCH_PARENT;
                ((TextView)v).setGravity(Gravity.RIGHT);
            }
    }
    public void rtllater(ListView v){
        v.setOnHierarchyChangeListener(new ViewGroup.OnHierarchyChangeListener() {
            @Override
            public void onChildViewAdded(View view, View view1) {
                rtlView(view1);
            }
            @Override
            public void onChildViewRemoved(View view, View view1) {

            }
        });
    }

the result is like this image:right to left Preference

查看更多
成全新的幸福
4楼-- · 2019-02-06 09:47

After spending some time searching about our shared question, I found nothing useful. obviosly android api doesn't support RTL prefefrences. it means no support for persian/hebrew/arabic languages. But afterall I tried to find some way to right align an editText. I drived EditTextPreference class and implemented onCreateView method again. here is my code:

/**
 * Created by omid on 1/12/14.
 */
public class RtlTextPreference extends EditTextPreference {


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

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

        RelativeLayout layout = (RelativeLayout) ((LinearLayout) view).getChildAt(1);
        layout.setGravity(Gravity.RIGHT);
        return view;
    }
}

the rest of the preferences in the standard android api should be the same(btw I haven't tried yet). maybe someday we should fully implement rtl support in android preferences and host it on github. Hope this code snippet helps you.

查看更多
来,给爷笑一个
5楼-- · 2019-02-06 09:48

Alright, this might be quite late, but anyways if you are targeting Android 3.0 or higher, you should use PreferenceFragment with can be somehow made RTL.

So you should create a fragment in your code like this:

import android.os.Bundle;
import android.preference.PreferenceFragment;

public class SettingsFragment extends PreferenceFragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.preferences);
    }
}

now you should create an Activity that adds this fragment to its layout. You can create a layout for your activity and put the fragment in it via xml element or using the FragmentManager class. But the key to make the preferences Right-To-Left is to put a layout_direction="rtl" attribute in the layout file for a parent element of the fragment. i.e.

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    android:id="@+id/rootView"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layoutDirection="rtl"
    android:orientation="vertical">

    <fragment
        class="blah.blah.SettingsFragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</RelativeLayout>

Notice the android:layoutDirection="rtl" on the RelativeLayout element that fixes the problem.

Now add the Activity Java code like any other activity and add to your manifest file.

Hope it's not too late :)

查看更多
老娘就宠你
6楼-- · 2019-02-06 09:48

for preferences fragment you can make its container to have this attribute

android:layoutDirection="rtl"
查看更多
虎瘦雄心在
7楼-- · 2019-02-06 09:50

You could do that by using a custom layout with calling setLayoutResource(int layoutResId).

res/layout/preference_layout.xml: (Note that this is based on HoloEverywhere library org.holoeverywhere.preference.Preference)

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="false"
    android:gravity="center_vertical"
    android:minHeight="?attr/listPreferredItemHeight"
    android:paddingRight="@dimen/preference_item_padding_side"
    android:paddingLeft="?android:attr/scrollbarSize" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center"
        android:minWidth="@dimen/preference_icon_minWidth"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:minWidth="48dp"
            android:contentDescription="@string/emptyString"
            android:paddingRight="@dimen/preference_item_padding_inner" >
        </ImageView>
    </LinearLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:paddingBottom="6dip"
        android:gravity="right"
        android:layout_gravity="right"
        android:paddingLeft="@dimen/preference_item_padding_inner"
        android:paddingTop="6dip" >

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal"
            android:gravity="right"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceMedium" >
        </TextView>

        <TextView
            android:id="@+id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignRight="@id/title"
            android:gravity="right"
            android:layout_below="@id/title"
            android:maxLines="10"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="?android:attr/textColorSecondary" >
        </TextView>
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/widget_frame"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center"
        android:minWidth="@dimen/preference_widget_width"
        android:orientation="vertical" >
    </LinearLayout>

</LinearLayout>

In your custom Preference:

@Override
protected View onCreateView(ViewGroup parent)
{
    setLayoutResource(R.layout.preference_layout);

    return super.onCreateView(parent);
}
查看更多
登录 后发表回答