我正在尝试将摘要添加到我的列表项之一。 (“ Systemstandardeinstellung”下的摘要)
我该怎么办? 我还没有找到任何解决方案,我发现的唯一发现就是如何使用当前选择的ListItem作为ListPreference摘要。
这就是我所拥有的。
preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<PreferenceCategory
android:key="basic_settings_category"
android:title="Grundeinstellungen"
app:iconSpaceReserved="false">
<ListPreference
android:defaultValue="0"
android:key="@string/theme_preferences_key"
android:title="Designs"
app:entries="@array/themes_entries"
app:entryValues="@array/themes_values"
app:iconSpaceReserved="false" />
<SwitchPreference
android:key="night_mode"
android:title="Nachtmodus"
app:iconSpaceReserved="false" />
</PreferenceCategory>
</PreferenceScreen>
theme_res.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="themes_entries">
<item>Systemstandardeinstellung</item>
<item>Hell</item>
<item>Dunkel</item>
</string-array>
<string-array name="themes_values">
<item>@string/system_theme_preference_value</item>
<item>@string/light_theme_preference_value</item>
<item>@string/dark_theme_preference_value</item>
</string-array>
</resources>
strings.xml
<resources>
<string name="app_name">TestApp</string>
<string name="settings">Settings</string>
<string name="openNavDrawer">Navigation Drawer Open</string>
<string name="closeNavDrawer">Navigation Drawer Close</string>
<string name="theme_preferences_key">theme_preferences_key</string>
<string name="notification_preferences_key"></string>
<string name="system_theme_preference_value">0</string>
<string name="light_theme_preference_value">1</string>
<string name="dark_theme_preference_value">2</string>
<string name="system_theme_description">Systemstandardeinstellung</string>
<string name="light_theme_description">Hell</string>
<string name="dark_theme_description">Dunkel</string>
</resources>
答案 0 :(得分:0)
最好的机会是使用带有自定义适配器的自定义项的对话框。一种简单的方法是使用将spinnermode设置为dalog的微调器,并用自定义项填充它。
在活动布局中添加此微调器
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dialog"
android:id="@+id/test_spn"/>
具有自定义项目类
public class custom_item {
public String name,description;
public int id;
public custom_item(String name, String description)
{
this.name=name;
this.description=description;
}
public custom_item(int id, String name, String description)
{
this.name=name;
this.description=description;
this.id=id;
}
}
在您的布局文件夹中,拥有您的自定义项目布局文件(custom_item_layout)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:orientation="vertical"
android:padding="5dp"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:textStyle="bold"
android:id="@+id/title"
android:layout_height="wrap_content"
android:text="Title"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/description"
android:textSize="10dp"
android:text="description"
android:layout_marginLeft="10dp"/>
</LinearLayout>
然后,您的适配器类进行适配(custom_adapter)
public class custom_adapter extends BaseAdapter {
ArrayList<custom_item> pref_items;
Context contxt;
public custom_adapter(Context contxt, ArrayList<custom_item> pref_items)
{
this.contxt=contxt;
this.pref_items=pref_items;
}
@Override
public int getCount() {
return pref_items.size();
}
@Override
public Object getItem(int position) {
return pref_items.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
convertView=null;
custom_item item= pref_items.get(position);
convertView= LayoutInflater.from(contxt).inflate(R.layout.custom_item_layout,null,false);
TextView title_=(TextView)convertView.findViewById(R.id.title);
TextView description=(TextView)convertView.findViewById(R.id.description);
title_.setText(item.name);
description.setText(item.description);
return convertView;
}
}
因此,在所有设置均已设置的情况下,只需在选择时将项目的ID或索引保存到共享首选项中即可。 将其放在您活动的oncreate
中 Spinner my_spn=((Spinner)findViewById(R.id.test_spn));
final String sp_name="SharedPrefS_name";
SharedPreferences prefs = getSharedPreferences(sp_name, MODE_PRIVATE);
ArrayList<custom_item> items=new ArrayList<custom_item>();
items.add(new custom_item("Systemstandardeinstellung","Description for the first title (Systemstandardeinstellung)" ));
items.add(new custom_item("Hell","Description for the second title (Hell)" ));
items.add(new custom_item("Dunkell","Description for the third title (Dunkell)" ));
my_spn.setAdapter(new custom_adapter(this,items));
my_spn.setSelection(prefs.getInt("my_item_identifier", 0));
my_spn.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
SharedPreferences.Editor saver = act.getSharedPreferences(sp_name, MODE_PRIVATE).edit();
saver.putInt("my_item_identifier", position);
saver.commit();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});