自定义ListView的奇怪行为

时间:2014-10-27 08:36:39

标签: android listview

我向ListView提出了一个问题,我想自定义。 ListView应该提供RingtonePreference的功能。在大多数情况下,这非常有效,但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

1 个答案:

答案 0 :(得分:0)

当你有一个固定高度的ListView时你有什么期望?

使用重量或使ListView占用屏幕的其余部分:

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