【发布时间】:2011-07-02 21:59:47
【问题描述】:
我希望能够从我的 PreferenceActivity 启动第二个 Preference 屏幕。在第二个首选项屏幕中,我想使用 xml 中的预定义布局。所以,我有两个问题:
如何使用 xml 布局作为 Preference 的布局视图? 如何将此自定义首选项添加到在点击时启动的 PreferenceActivity?
谢谢
*针对不在场证明进行编辑
我正在尝试通过在 xml 中声明要启动的活动来从首选项屏幕启动活动。这会导致此异常:
04-01 19:04:37.962: ERROR/AndroidRuntime(8061): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.me/CustomPrefScreen}; have you declared this activity in your AndroidManifest.xml?
*另一个更新。但是,如果我将 settings.xml 中的 PrefrenceScreen 替换为 Preference 的某些扩展名,它会覆盖 onClick() 以启动 CustomPrefScreen,那么一切正常。
主要偏好活动:
public class MyPreferences extends PreferenceActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}
settings.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceScreen
android:summary="my summary"
android:title="my title">
<intent android:action="android.intent.action.MAIN"
android:targetPackage="com.me"
android:targetClass="CustomPrefScreen"/>
</PreferenceScreen>
</PreferenceScreen>
主文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.me"
android:versionCode="1"
android:versionName="1.0">
<application
android:icon="@drawable/icon"
android:label="@string/app_name"
android:theme="@style/Theme.NoBackground">
<activity
android:name=".MyApp"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".CustomPrefScreen"
android:label="@string/app_name">
</activity>
<activity
android:name=".MyPreferences"
android:label="@string/app_name">
</activity>
</application>
<uses-sdk android:minSdkVersion="4" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
【问题讨论】:
-
这两件事都在 Api Demos 示例中得到了很好的解释。有趣的是它在 SDK 目录中,因此您的硬盘中现在就有示例。
-
您是否绝对需要其他偏好作为第二个活动?如果您只想使用多个屏幕,或许只需使用 PreferenceScreen 工作?
-
@Slund。我想使用 PreferenceScreen,但我不确定如何使用自定义 xml 布局;好像只能添加 Preference 类型的视图?
-
啊哈。现在我明白了这个问题。您希望有一个使用自定义 UI 而不是标准 FooPreference 视图的首选项屏幕。为此,您很可能希望自己的 MyPreference 扩展 Preference。
-
@Slund。嗯,这就是我想知道该怎么做。我可以为 MyPreference 定义布局,单击它时会启动它吗?或者 MyPreference 是否必须启动一个使用我的自定义布局的新活动?
标签: android preferences