1

我有一个使用自定义 ListPreference 和 CheckBoxPreference 制作的 PreferenceScreen。这是名为“pref_screen_custom.xml”的 XML:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

<!-- My custom preference type.  This just replaces the actual widget
     portion of the preference, if the whole preference wanted to be
     replaced we would use the layout attribute instead of the widgetLayout
     attribute. -->
<com.pref_002.Pref002
        android:key="my_preference"
        android:title="Title: Pref002"
        android:summary="Summary: summary"
        android:defaultValue="-1"
        android:entries="@array/pref002_list_ent"
        android:entryValues="@array/pref002_list_vals"
        android:dialogTitle="title" />

<CheckBoxPreference
        android:key="advanced_checkbox_preference"
        android:title="Title: checkbox preference"
        android:summaryOn="On checkbox" 
        android:summaryOff="Off checkBox" />
</PreferenceScreen>

保存首选项数组的 XML 是 (array.xml):

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string-array name="pref002_list_ent">
        <item>T</item>
        <item>V</item>
        <item>S</item>
    </string-array>
    <string-array name="pref002_list_vals">
        <item>10</item>
        <item>8</item>
        <item>6</item>
    </string-array>

</resources>

PreferenceScreen 的 .java,称为 PrefCustom002Activity.java,是:

package com.pref_002;

import android.os.Bundle;
import android.preference.PreferenceActivity;

public class PrefCustom002Activity extends PreferenceActivity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.pref_screen_custom_001);
    }
}

扩展 ListPreference 的类,称为“Prefs002.java”,是:

package com.pref_002;

import android.content.Context;
import android.preference.ListPreference;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

public class Pref002 extends ListPreference
{
    // Constructor called by the inflater
    public Pref002(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }
    @Override
    protected View onCreateView(ViewGroup parent)
    {
        // New Layout that will contain the default loaded View from ListPreference
        // plus a new one (a Button)
        LinearLayout newLayoutParent = new LinearLayout(getContext());
        newLayoutParent.setOrientation(LinearLayout.HORIZONTAL);
        newLayoutParent.setWeightSum(10.0f);

        // get the View returned from ListPreference 
        View listPreferenceDefaultView = super.onCreateView(parent);

        // new layout values for this View
        LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        params1.weight = 9.0f;

        listPreferenceDefaultView.setLayoutParams(params1);

        //--Button to be added to newLayoutParent --// 

        LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        params2.gravity = Gravity.CENTER_VERTICAL;
        params2.weight = 1.0f;

        Button b = new Button(getContext());
        b.setText("A button");
        b.setLayoutParams(params2);


        // add the two views
        newLayoutParent.addView(listPreferenceDefaultView);
        newLayoutParent.addView(b);
        newLayoutParent.setId(android.R.id.widget_frame);

        return newLayoutParent;
    }
}

因此,正如您在“Prefs002.java”中看到的那样,我覆盖 onCreateView 为 ListPreference 制作一个新布局,该布局由一个 LinearLayout 组成,其中包含 ListPreference 自己创建的 View 以及一个位于右侧的新按钮ListPreference 生成的视图。从图形上看,这是可行的,但是如果我单击此新视图,则不会显示“首选项”对话框(当您单击它时,该行也不会变为黄色,但我可以通过添加 TouchListener 来解决此问题)。此外,根据文档,如果我覆盖 OnCreateView,我应该将“widget_frame”设置为新视图,并且我在该行 newLayoutParent.setId(android.R.id.widget_frame); 中进行了操作,但是当我单击此视图时不会出现对话框。

那么,如何调用这个自定义 ListPrerence 的 Dialog 呢?

提前致谢。

4

1 回答 1

0

您可以通过调用 showDialog() (DialogPreference.showDialog) 来触发对话框。当您调用 super.onCreateView 时,返回的视图将已经有一个 ID 为 widget_frame 的视图。

我会建议这样的事情:

View v = super.onCreateView(parent);
View widgetFrame = v.findViewById(android.R.id.widget_frame);
// add/remove stuff into widgetFrame here
return v;
于 2012-09-14T18:28:50.753 回答