How to right align PreferencesActivity in android?

前端 未结 14 2206
一向
一向 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: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;
    }
    

    }

提交回复
热议问题