3

我有这样一段代码:) 我想编辑我的自定义首选项布局的文本属性。但是从 getView 函数对对象所做的任何更改都不会影响首选项屏幕中的实际列表。有任何想法吗?我知道我不能扩展 PreferenceScreen,在这种情况下我不能使用任何其他类型的首选项,我只想能够从我的代码布局中编辑我的自定义文本视图。

PreferenceScreen settings = getPreferenceManager().createPreferenceScreen(this);

settings.setLayoutResource(R.layout.mypreference);
View test = (View) settings.getView(null,getListView());

TextView text = (TextView)test.findViewById(R.id.text);
text.setText("BLA BLA");    
4

4 回答 4

8

我们应该使用getView(convertView, parent),但从 StackOverflow 上无数未回答的问题来看,很明显没有人知道如何使用它。所以我们唯一的选择是创建一个自定义Preference

创建一个新类:

public class CustomPreference extends Preference {
    public CustomPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onBindView(View rootView) {
        super.onBindView(rootView);

        View myView = rootView.findViewById(R.id.myViewId);
        // do something with myView
    }
}

在您的 xml/preferences.xml中,添加您的自定义首选项:

<your.package.name.CustomPreference
    android:key="custom_preference"
    android:layout="@layout/custom_layout" />
于 2015-01-29T04:51:18.647 回答
0

使用下面的自定义布局:

custom_preferences_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:orientation="vertical">
    <Button 
        android:id="@+id/preview_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:text="@string/notification_preview" />
    <ListView 
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

将上面的文件与您的用户首选项 xml 文件一起使用,您可以执行此操作来访问自定义布局中的项目:

public class CustomPreferenceActivity extends PreferenceActivity {

    @Override
    protected void onCreate(Bundle bundle){
        super.onCreate(bundle);
        addPreferencesFromResource(R.xml.custom_preferences);
        setContentView(R.layout.custom_preferences_layout);
    }

    private void initCustomizePreferences(){
        //Button
        Button button = (Button)findViewById(R.id.preview_button);
        //Do something with the button.

    }

}

我希望这有帮助 :)

于 2013-03-14T16:25:09.767 回答
0

您需要onBindView在自定义Preference类中覆盖。

更多详细信息 - 使用自定义布局获取偏好视图

于 2014-09-27T12:47:43.253 回答
-2

我从来没有真正弄乱过 PreferenceActivity 的视图,但这是我做的事情(通常用于 ListPreferences)

ListPreference listPref;
CheckBoxPreference cbPref;

// Set the summary as the current listpref value
listPref = (ListPreference) findPreference("list_pref_key");
listPref.setSummary(listPref.getEntry()); 

// change the title / summary / ??? of any preference
findPreference("anothe_pref_key").setTitle("New Title");
findPreference("anothe_pref_key").setSummary("New subtext for the view");

这样,即使 PreferenceActivity / PreferenceFragment 使用的布局在未来版本中发生变化,您也不必更改代码中的任何内容。

于 2012-08-30T07:53:02.470 回答