2013-06-25 26 views
7

我需要创建一个自定义ListPreference对话框,以便我可以在List(ListView)上添加一些标题文本(一个TextView)。 扩展ListPreference和覆盖onCreateDialogView()我已经创建MyListPreference类:为什么ListView保持在ListPreference对话框的TextView之上?

@Override 
protected View onCreateDialogView() { 
    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View v = (View) inflater.inflate(R.layout.dialog_preference_list, null); 
    return v; 
} 

我的XML布局dialog_preference_list.xml包含:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="TextView" /> 

    <ListView 
     android:id="@android:id/list" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:drawSelectorOnTop="false" 
     android:scrollbarAlwaysDrawVerticalTrack="true" /> 

</LinearLayout> 

问题:TextView中显示的ListView,而不是下面以上。我需要上面的TextView。我已经尝试了LinearLayout和RelativeLayout(使用“below”或“above”属性),但都没有成功:我无法找到将TextView放在ListView之上的方法...布局非常简单,我看不到为什么列表保持在... 另外,请注意,问题发生在真实设备(Nexus 4,Android 4.2.2)和仿真器上。但是,在查看Eclipse图形布局中呈现的布局时,布局是正确的!看到两个附加的图片。

关于如何解决这个问题的任何想法?

布局呈现的设备(不正确的)上:

Layout rendered on the device (incorrect)

布局呈现于Eclipse(正确):

Layout rendered on Eclipse (correct)

编辑与溶液2013年10月7日

正如接受的答案所示,问题来自于ListPreference的onPrepareDialogBu​​ilder()中builder.setSingleChoiceItems()的使用。 我已经通过扩展ListPreference并覆盖onCreateDialogView()来修复它,以在没有构建器的情况下构建对话框,以便我可以创建自定义视图,以显示列表项上方的标题文本。

GPListPreference.java:

public class GPListPreference extends ListPreference { 
    ... 

    @Override 
    protected void onPrepareDialogBuilder(AlertDialog.Builder builder) { 
     builder.setNegativeButton(null, null); 
     builder.setPositiveButton(null, null); 
    } 

    private int getValueIndex() { 
     return findIndexOfValue(getValue()); 
    } 

    @Override 
    protected View onCreateDialogView() { 
     LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     ListView lv = (ListView) inflater.inflate(R.layout.dialog_preference_list, null); 

     TextView header = (TextView) inflater.inflate(R.layout.dialog_preference_list_header, null); 
     header.setText(getDialogMessage()); // you should set the header text as android:dialogMessage in the preference XML 
     lv.addHeaderView(header); 

     ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(getContext(), R.layout.dialog_preference_list_singlechoice, getEntries()); 
     lv.setAdapter(adapter); 

     lv.setClickable(true); 
     lv.setEnabled(true); 
     lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
     lv.setItemChecked(getValueIndex() + 1, true); 
     lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       setValueIndex(position - 1); 
       getDialog().dismiss(); 
      } 
     }); 

     return lv; 
    } 
} 

dialog_preference_list.xml:

<?xml version="1.0" encoding="utf-8"?> 
<ListView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/list" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:drawSelectorOnTop="false" 
    android:scrollbarAlwaysDrawVerticalTrack="true" /> 

dialog_preference_list_singlechoice.xml

<?xml version="1.0" encoding="utf-8"?> 
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:checkMark="?android:attr/listChoiceIndicatorSingle" 
    android:ellipsize="marquee" 
    android:gravity="center_vertical" 
    android:minHeight="?android:attr/listPreferredItemHeight" 
    android:paddingBottom="2dip" 
    android:paddingLeft="10dip" 
    android:paddingRight="10dip" 
    android:paddingTop="2dip" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

dialog_preference_list_header.xml

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="10dip" 
    android:textAppearance="?android:attr/textAppearanceSmall"> 

</TextView> 
+0

能在您初始化值,你添加代码的名单?或任何引用ListView的代码 – Muzikant

+0

为了清楚起见,你已经试过了:android:layout_above =“@ + id/list”' 和可选的'android:layout_alignParentTop =“true”'原始dialog_preference_list.xml中的T​​extView, 对不对? – JaKXz

+0

太棒了!谢谢你muslidrikk。 – Tiago

回答

4

我认为问题与ListPreference的工作方式有关。 ListPreference使用Builder.setSingleChoiceItems()来创建具有RadioButtons的行,并且它优先于您要添加的自定义布局(在您的情况下是LinearLayout内部的TextView和ListView,解决方案是扩展DialogPreference而不是Here 。到GitHub的一个链接,我创建了一个自定义的DialogPreference已经做了你需要什么,我还没有编码的单选按钮逻辑

+0

这对你有帮助吗? – Emmanuel

+0

感谢您的建议。事实上,问题来自Builder.setSingleChoiceItems()。我已通过扩展ListPreference并覆盖onCreateDialogView()来修复它,以不使用对话框构建器。我将把代码放在一个单独的答案中。 – muslidrikk

+0

好,我能够帮助。 GitHub链接是否可以帮助你? – Emmanuel

2

我想这是一个主题化的问题。尝试在构造函数中更改对话框的主题,使其类似于setStyle(STYLE_NO_TITLE, R.style.AppTheme)。您的基本应用程序主题no_title风格。

如果这不是问题,它可能与ListPreference类本身有关。它可能会覆盖您的布局以保持偏好视图的一致性。不过,我之前没有使用ListPreference,所以它只是一个猜测。

您是否可以通过播放XML图形布局预览中的主题来重现相同的结果?

+0

谢谢你的建议。然而,在玩过主题后,它不是问题。 – muslidrikk

1

你可以尝试另一种选择是将TextView作为头添加到ListView这样的:

TextView textView = new TextView(getActivity()); 
ListView listView = new ListView(getActivity()); 
listView.addHeaderView(textView); 

addHeaderView需要View所以你理论上有任何东西你想成为头,但我只使用了TextView

+0

谢谢,但在ListPreference的ListView上使用addHeaderView()似乎不起作用... – muslidrikk

0

上面的链接已损坏。在这个解决方案中,这个想法是重写ListPreference,并用ListPreference上定义的数据来扩充自己的listview。

@Override 
protected View onCreateDialogView() { 

    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    ListView lv = new ListView(getContext()); 

    // Inflate the view into the header only if a message was set 
    if (getDialogMessage() != null && ! getDialogMessage().equals("")) { 

     TextView header = (TextView) inflater.inflate(R.layout.dialog_preference_list_header, null); 
     header.setText(getDialogMessage()); 
     lv.addHeaderView(header, null, false); 

    } 

    // Create a new adapter and a list view and feed it with the ListPreference entries 
    ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(getContext(), 
      R.layout.custom_dialog_single_choice_list_adapter, getEntries()); 

    lv.setAdapter(adapter); 
    lv.setClickable(true); 
    lv.setEnabled(true); 
    lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
    lv.setItemChecked(getValueIndex() + 1, true); 
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      setValueIndex(position - 1); 
      getDialog().dismiss(); 
     } 
    }); 
    return lv; 
} 

另一个重要的事情是调用onPrepareDialogBu​​ilder而不是调用super。这将避免列表视图出现两次。

@Override 
protected void onPrepareDialogBuilder(AlertDialog.Builder builder) { 
    // Not calling super, to avoid having 2 listviews 

    // Set the positive button as null 
    builder.setPositiveButton(null, null); 
} 

private int getValueIndex() { 
    return findIndexOfValue(getValue()); 
} 

凡dialog_preference_list_header是在我的情况下,只有一个TestView,但它可能是一个更复杂的视图,并custom_dialog_single_choice_list_adapter可能是这样的:

<?xml version="1.0" encoding="utf-8"?> 
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:checkMark="?android:attr/listChoiceIndicatorSingle" 
    android:ellipsize="marquee" 
    android:gravity="center_vertical" 
    android:minHeight="?android:attr/listPreferredItemHeight" 
    android:paddingBottom="2dip" 
    android:paddingLeft="10dip" 
    android:paddingRight="10dip" 
    android:paddingTop="2dip" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 
相关问题