How to right align PreferencesActivity in android?

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

    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!

    0 讨论(0)
  • 2020-12-31 07:37
    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;
    }
    

    }

    0 讨论(0)
  • 2020-12-31 07:38

    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?

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-31 07:44
    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;
    }
    

    }

    0 讨论(0)
  • 2020-12-31 07:44

    This method worked for me:

            @Override
    public View onCreateView(View parent, String name, Context context, AttributeSet attrs)
    {
        View v = super.onCreateView(parent, name, context, attrs);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
        {
            if(parent != null)
                parent.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
            if(v != null)
                v.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
        }
        return v;
    }
    
    0 讨论(0)
提交回复
热议问题