Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android上的PreferenceScreen中添加RTL支持_Android_Xml_Android Manifest - Fatal编程技术网

如何在Android上的PreferenceScreen中添加RTL支持

如何在Android上的PreferenceScreen中添加RTL支持,android,xml,android-manifest,Android,Xml,Android Manifest,我想在设置中使用首选项屏幕,但无法将RTL设置为此XML文件。我试过使用这个代码,但它不起作用 Manifest中的android:supportsRtl=“true” Minsdk第15版。 我的XML代码: <?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" android:orienta

我想在设置中使用
首选项屏幕
,但无法将
RTL
设置为此XML文件。我试过使用这个代码,但它不起作用

Manifest中的android:supportsRtl=“true”

Minsdk第15版。

我的XML代码:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical">
    <PreferenceCategory android:title="@string/action_settings">
        <Preference
            android:key="prefDeleteAll"
            android:summary="@string/settings_delete_all_description"
            android:title="@string/settings_delete_all" />
        <Preference
            android:key="prefCustomPath"
            android:title="@string/settings_custom_path" />
        <ListPreference
            android:defaultValue="1"
            android:entries="@array/filenameEntries"
            android:entryValues="@array/filenameValues"
            android:key="prefCustomFilename"
            android:title="@string/settings_custom_filename" />
        <ListPreference
            android:defaultValue="1"
            android:entries="@array/sortEntries"
            android:entryValues="@array/sortValues"
            android:key="prefSortMode"
            android:title="@string/settings_sort_mode" />
    </PreferenceCategory>
    <PreferenceCategory android:title="@string/settings_customizations">
        <yuku.ambilwarna.widget.AmbilWarnaPreference
            android:defaultValue="@color/primary"
            android:key="prefPrimaryColor"
            android:summary="@string/settings_primary_color_description"
            android:title="@string/settings_primary_color" />
        <yuku.ambilwarna.widget.AmbilWarnaPreference
            android:defaultValue="@color/fab"
            android:key="prefFABColor"
            android:summary="@string/settings_fab_color_description"
            android:title="@string/settings_fab_color" />
        <CheckBoxPreference
            android:defaultValue="false"
            android:key="prefNavigationBlack"
            android:summary="@string/settings_navigation_black_description"
            android:title="@string/settings_navigation_black" />
        <Preference
            android:key="prefDefaultValues"
            android:summary="@string/settings_default_customization_description"
            android:title="@string/settings_default_customization" />
    </PreferenceCategory>
    <PreferenceCategory android:title="@string/action_about">
        <Preference
            android:key="prefLicense"
            android:title="@string/settings_license" />
        <Preference
            android:key="prefVersion"
            android:summary="@string/settings_about"
            android:title="@string/app_name" />
    </PreferenceCategory>
</PreferenceScreen>



如何修复它?

有一个技巧如何解决,因为基本上PreferenceScreen不支持RTL

最简单的方法是将下一个代码放在“创建首选项”活动中:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
        getListView().setLayoutDirection(View.LAYOUT_DIRECTION_LOCALE);
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
     getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);

这就应该做到了

我覆盖了
首选项#onCreateView(ViewGroup)
,并将一些RTL规则应用于从
super
返回的
视图

下面是一个
EditTextPreferenceRTL
的实现,其行为类似于
EditTextPreference
,但其“标题”和“摘要”显示在Right-To-Left:

public class EditTextPreferenceRTL extends EditTextPreference {

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    EditTextPreferenceRTL(Context context, AttributeSet attrs, int defStyleAttr,
                          int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    EditTextPreferenceRTL(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    EditTextPreferenceRTL(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    EditTextPreferenceRTL(Context context) {
        super(context);
    }

    @Override
    protected View onCreateView(ViewGroup parent) {

        final View view = super.onCreateView(parent);

        final int titleIndex = 0;
        final int summaryIndex = 1;

        RelativeLayout layout = (RelativeLayout) ((LinearLayout) view).getChildAt(summaryIndex);
        RelativeLayout.LayoutParams lpTitle = new RelativeLayout.LayoutParams(view.getLayoutParams());
        RelativeLayout.LayoutParams lpSummary = new RelativeLayout.LayoutParams(view.getLayoutParams());

        lpTitle.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        lpSummary.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

        TextView tvTitle = (TextView) layout.getChildAt(titleIndex);
        tvTitle.setLayoutParams(lpTitle);

        lpSummary.addRule(RelativeLayout.BELOW, tvTitle.getId());

        TextView tvSummary = (TextView) layout.getChildAt(summaryIndex);
        tvSummary.setLayoutParams(lpSummary);

        return view;
    }
}


此代码不向前兼容,在将来的版本中可能会中断,
EditTextPreference
中可能会发生更改,
EditTextPreferenceRTL
扩展了

您可以将此代码与AppCompatPreferenceActivity一起使用:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
        getListView().setLayoutDirection(View.LAYOUT_DIRECTION_LOCALE);
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
     getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);

您可以向我显示首选项活动代码?使用以下链接下载我的代码:。tnx请帮助我您尝试过在“super.onCreate(savedInstanceState);”之后和“addPreferencesFromResource(R.xml.settings);”之前添加我给您的代码吗?是的,super.onCreate(savedInstanceState);如果(Build.VERSION.SDK\u INT>=Build.VERSION\u code.JELLY\u BEAN\u MR1)getListView().setLayoutDirection(View.LAYOUT\u DIRECTION\u LOCALE);addPreferencesFromResource(R.xml.settings)。但不起作用:(.我需要这个IF设置语言动态地为我工作:getListView().setLayoutDirection(View.LAYOUT\u DIRECTION\u RTL);