我想在我的设置中使用 PreferenceScreen
,但我无法将RTL
设置为此XML文件。我已尝试使用此代码,但它不起作用!
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>
如何解决?
答案 0 :(得分:1)
我覆盖Preference#onCreateView(ViewGroup)
并将一些RTL规则应用于View
返回的super
。
以下是EditTextPreferenceRTL
的实现,其行为类似于EditTextPreference
,但其“标题”和“摘要”显示自 R ight- T < /强>邻的→强> EFT:
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
可能会发生变化。
答案 1 :(得分:0)
有一个技巧怎么做,因为基本的PreferenceScreen不支持RTL。
最简单的方法是将下一个代码放在PreferenceActivity的onCreate中:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
getListView().setLayoutDirection(View.LAYOUT_DIRECTION_LOCALE);
那应该做的伎俩
答案 2 :(得分:0)
您可以将此代码与AppCompatPreferenceActivity结合使用:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);