How to right align PreferencesActivity in android?

前端 未结 14 2246
一向
一向 2020-12-31 07:07

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\"

14条回答
  •  臣服心动
    2020-12-31 07:50

    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.

    
    
    
    
        
    
    

    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 :)

提交回复
热议问题