2014-10-27 116 views
-1

我收到了一個ListView的問題,我想對其進行自定義。 ListView應該提供RingtonePreference的功能。大多數情況下,這種方式運行得非常好,但ListView向用戶顯示的方式並不一致。我嘗試了不同的方法,但是我的自定義ListView分成兩部分,或者對話框的按鈕超出範圍。見下圖下來..自定義ListView的奇怪行爲

這裏是我的代碼:

@Override 
     protected View onCreateDialogView() { 
     SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mContext); 
     SharedPreferences.Editor editor = preferences.edit(); 
     String mThemeSettings = MyVars.THEME_KEY_DEFAULT; 

     // Theme-Einstellungen 
     if (preferences.contains(MyVars.SETTING_THEME)) { 
      mThemeSettings = preferences.getString(MyVars.SETTING_THEME, yVars.THEME_KEY_DEFAULT); 
     } else { 
      editor.putString(MyVars.SETTING_THEME, mThemeSettings); 
      editor.apply(); 
     } 

     // View für Rückgabe anlegen 
     View view; 

     if (mThemeSettings.equals(MyVars.THEME_KEY_2)) { 
      view = View.inflate(mContext, R.layout.custom_list_preference_black, null); 
     } else view = View.inflate(mContext, R.layout.custom_list_preference_white, null); 

     mDialogTitle = getDialogTitle(); 
     if (mDialogTitle == null) mDialogTitle = getTitle(); 
     ((TextView) view.findViewById(R.id.dialog_title)).setText(mDialogTitle); 

     ListView list = (ListView) view.findViewById(android.R.id.list); 


     ArrayAdapter<CharSequence> adapter; 
     if (mThemeSettings.equals(MyVars.THEME_KEY_2)) { 

      adapter = new ArrayAdapter<CharSequence>(
        getContext(), R.layout.btn_radio_black, 
        getEntries()); 
     } else { 
      adapter = new ArrayAdapter<CharSequence>(
        getContext(), R.layout.btn_radio_white, 
        getEntries()); 
     } 

     // Adapter setzen 
     list.setAdapter(adapter); 
     // Achtung: vorher Clickable setzen, sonst werden die Vorgaben nicht angenommen... 
     list.setClickable(true); 
     list.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
     list.setItemChecked(findIndexOfValue(mPath), true); 
     list.setOnItemClickListener(this); 

     return view; 
    } 


    @Override 
    protected void onPrepareDialogBuilder(@NonNull Builder builder) { 
     super.onPrepareDialogBuilder(builder); 

     // Holo-Titel unsichtbar machen 
     builder.setTitle(null); 

     mMediaPlayer = new MediaPlayer(); 
     mEntries = getEntries(); 
     mEntryValues = getEntryValues(); 

     builder.setPositiveButton(mContext.getString(R.string.btn_ok), this); 
     builder.setNegativeButton(mContext.getString(R.string.btn_cancel), this); 

    } 

和XML:

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

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="500dp" 
     android:orientation="vertical"> 

     <TextView 
      android:id="@+id/dialog_title" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="16dp" 
      android:textColor="@color/white" 
      android:textSize="22sp"/> 

     <View 
      android:layout_width="match_parent" 
      android:layout_height="8dp" 
      android:background="@color/blue_app_color"/> 

     <ListView 
      android:id="@android:id/list" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:listSelector="@color/blue_app_color"/> 

    </LinearLayout> 

</LinearLayout> 

enter image description here

enter image description here

回答

0

什麼你當你有期待一個固定的高度ListView?

要麼使用重量或讓你的ListView佔用屏幕的其餘部分用:

<ListView 
    ... 
    android:layout_height="match_parent" 
    .../> 
+0

啊,我只是複製了錯誤的代碼: - /按鈕離開屏幕時,我ListView的匹配父。 .. – 2014-10-27 08:48:45

+2

@MartinPfeffer用*正確的*代碼更新你的問題。 – Simas 2014-10-27 09:16:56