How to right align PreferencesActivity in android?

前端 未结 14 2249
一向
一向 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:42

    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

提交回复
热议问题