我制作了一个自定义首选项布局,每行有2个切换,称为dualtogglepreference。与一个扩展Preference的类一起处理它的一些细节。当我将此自定义首选项添加到我的preferences.xml文件时,它将显示在UI中,但我无法使用“首选项活动”中的findPreference引用它。
preferences.xml文件
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="Notifications">
<com.hitpost.testing.DualTogglePreference
android:key="followsMe"
android:title="Someone follows me"
android:layout="@layout/dualtogglepreference"/>
</PreferenceCategory>
</PreferenceScreen>
PreferenceActivity
public class TestingCustomPreferenceActivity extends PreferenceActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
DualTogglePreference followsMe = (DualTogglePreference) findPreference("followsMe");
if (followsMe != null)
Log.e("FOLLOWS_ME", "NOT NULL");
else
Log.e("FOLLOWS_ME", "NULL"); //THIS IS PRINTED
}
}
视觉上一切看起来都很完美,即窗口小部件的布局是正确的。请帮忙,最后一天一直在争夺这个。
答案 0 :(得分:2)
就我而言,我忽略了定义将由充气机使用的构造函数。
public class StaticDialogPreference extends DialogPreference {
// this constructor is called by the infaltor
public StaticDialogPreference(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public StaticDialogPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
setDialogMessage(getContext().getString(R.string.static_message));
setNegativeButtonText(null);
}
}