我正在开发一个包含自定义PreferenceActivity
视图的Preference
。我的问题是我用ListView
创建了一个视图,它只显示了第一个元素。我发布了我的代码和图片:
http://imageshack.us/photo/my-images/545/sc20120307161530.png/
http://img545.imageshack.us/img545/7207/sc20120307161530.png'边界=' 0' />
的xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
android:key="player_settings"
android:title="@string/settings_player_config" >
<EditTextPreference
android:defaultValue="@string/settings_player_default_name"
android:dialogMessage="@string/settings_player_summary"
android:dialogTitle="@string/settings_playersname"
android:key="player_name"
android:summary="@string/settings_player_summary"
android:title="@string/settings_playersname" />
</PreferenceCategory>
<PreferenceCategory
android:key="volume"
android:title="@string/settings_volume" >
<com.battleship.preferences.SeekBarPreferences
android:defaultValue="50"
android:key="volume"
android:title="@string/settings_volume" />
</PreferenceCategory>
<PreferenceCategory
android:key="shine"
android:title="@string/settings_shine" >
<com.battleship.preferences.SeekBarPreferences
android:defaultValue="50"
android:key="shine"
android:title="@string/settings_shine" />
</PreferenceCategory>
<PreferenceCategory
android:key="themeTitle"
android:title="@string/settings_group_themes" >
<com.battleship.preferences.ListPreferences android:key="theme" />
</PreferenceCategory>
<PreferenceCategory
android:key="fontsTitle"
android:title="@string/settings_group_font_size" >
<com.battleship.preferences.ListPreferences android:key="font" />
</PreferenceCategory>
</PreferenceScreen>
Custom ListPreference:
package com.battleship.preferences;
import com.battleship.R;
import android.content.Context;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.preference.Preference;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;
public class ListPreferences extends Preference implements
OnCheckedChangeListener {
public ListPreferences(Context context) {
super(context);
}
public ListPreferences(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ListPreferences(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onClick() {
super.onClick();
Toast t = Toast.makeText(getContext(), "HOLA!", 3);
t.show();
}
@Override
protected View onCreateView(ViewGroup parent) {
String[] contentString = new String[3];
if (getKey().equals("theme")) {
contentString = new String[] {
(getContext().getString(R.string.settings_theme_default)),
(getContext().getString(R.string.settings_theme_black)),
(getContext().getString(R.string.settings_theme_white)) };
} else {
contentString = new String[] {
(getContext().getString(R.string.settings_font_big)),
(getContext().getString(R.string.settings_font_medium)),
(getContext().getString(R.string.settings_font_little)) };
}
ListView listView = new ListView(getContext());
ArrayAdapter<String> array = new ArrayAdapter<String>(getContext(),
android.R.layout.simple_list_item_single_choice,
android.R.id.text1, contentString);
listView.setAdapter(array);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
return listView;
}
private void updatePreference(int intRadio) {
SharedPreferences.Editor editor = getEditor();
editor.putInt(getKey(), intRadio);
editor.commit();
}
}
答案 0 :(得分:5)
当每个人都试图告诉你不应该这样做而不是回答你的问题时,你不讨厌它吗?能够动态生成内容对于应用灵活性至关重要,我完全知道你在追求什么。
这将有效,假设所有ListView条目的高度大致相同(省略了非相关部分)。订单很重要,尽量按照我的指示保留:
import android.view.ViewGroup.OnHierarchyChangeListener;
public class ListPreferences extends Preference implements
OnCheckedChangeListener, OnHierarchyChangeListener {
private ListView listView;
private View thisView;
private int listHeight = 0;
@Override
protected View onCreateView(ViewGroup parent) {
this.setLayoutResource(R.layout.listview_preference_layout);
thisView = super.onCreateView(parent);
listView = (ListView) thisView.findViewById(android.R.id.list);
listView.setOnHierarchyChangeListener(this);
String[] contentString = new String[3];
if (getKey().equals("theme")) {
contentString = new String[] {
(getContext().getString(R.string.settings_theme_default)),
(getContext().getString(R.string.settings_theme_black)),
(getContext().getString(R.string.settings_theme_white)) };
} else {
contentString = new String[] {
(getContext().getString(R.string.settings_font_big)),
(getContext().getString(R.string.settings_font_medium)),
(getContext().getString(R.string.settings_font_little)) };
}
ArrayAdapter<String> array = new ArrayAdapter<String>(getContext(),
android.R.layout.simple_list_item_single_choice,
android.R.id.text1, contentString);
listView.setAdapter(array);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
return thisView;
}
public void onChildViewAdded(View parent, View child) {
int childHeight = child.getMeasuredHeight();
if(childHeight > 0)
{
listHeight = listView.getAdapter().getCount() * childHeight;
thisView.setMinimumHeight(listHeight);
Log.i(TAG,"onChildViewAdded, done: "+listHeight+" "+childHeight);
}
}
public void onChildViewRemoved(View parent, View child) {
}
}
您还需要res / layout / listview_preference_layout.xml,其中包含以下内容:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="fill_vertical|fill_horizontal|fill"
android:gravity="fill"
android:orientation="vertical"
>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:clipChildren="true"
android:isScrollContainer="true"
android:layout_gravity="fill_vertical|fill_horizontal|fill"
android:layout_weight="1"
android:choiceMode="singleChoice"/>
</LinearLayout
答案 1 :(得分:0)
问题是您正在尝试将ListView添加为另一个ListView的项目。 ListView用于PreferenceActivity的布局。因此它不起作用。
我不明白你为什么不想使用标准解决方案ListPreference。它完全是为了向用户呈现单选按钮列表。
答案 2 :(得分:0)
我建议您为“偏好设置”创建自定义布局。然后,您可以根据自己的喜好创建和设置首选项。使用SharedPreferences
中的PreferenceManager.getDefaultPreferences()
将允许您保存所有更改,就像使用普通的首选项解决方案一样。
正如马尔科姆所说,你应该坚持常规做法,而不是试图做任何复杂的事情。
另一个建议是,不要使用ListView。相反,如果您希望显示所有视图,请手动将这些视图添加到LinearLayout,然后将其放在“首选项视图”中。