gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:40:35 28 4
gpt4 key购买 nike

我想在我的设置中使用 PreferenceScreen,但我无法为这个 XML 文件设置 RTL。我试过这段代码,但它不起作用!

android:supportsRtl="true"Manifest

minSdkVersion 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>


如何解决?

最佳答案

我覆盖了 Preference#onCreateView(ViewGroup) 并将一些 RTL 规则应用于从 super 返回的 View

这是一个 EditTextPreferenceRTL 的实现,它的行为类似于 EditTextPreference,但它的“标题”和“摘要”是从R右显示的-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 extends.

关于android - 如何在 Android 的 PreferenceScreen 中添加 RTL 支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34566092/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com