android – 以编程方式创建布局首选项

前端之家收集整理的这篇文章主要介绍了android – 以编程方式创建布局首选项前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要偏好帮助.我需要以编程方式创建它们.我有这个偏好屏幕:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
    <PreferenceCategory android:title="About" >
        <Preference android:title="Software Version" android:summary="1.3.554" android:key="software"/>
        <Preference android:title="User" android:summary="jnovkovic@itekako.com" android:key="email"/>
        <Preference android:title="Terms and conditions" />
    </PreferenceCategory>
    <PreferenceScreen android:key="log_out" android:layout="@layout/preference_log_out"/>


</PreferenceScreen>

我这样做了:

PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);
PreferenceCategory about = new PreferenceCategory(this);
        about.setTitle("About");
        root.addPreference(about);

        Preference software = new Preference(this);
        software.setTitle("Software Version");
        software.setKey("software");
        software.setSummary("1.3.554");
        about.addPreference(software);

        Preference user = new Preference(this);
        user.setTitle("User");
        user.setKey("user");
        user.setSummary(preferences.getString("user_email",""));
        about.addPreference(user);

        Preference terms_and_conditions = new Preference(this);
        terms_and_conditions.setTitle("Terms and conditions");
        terms_and_conditions.setKey("terms_and_conditions");
        about.addPreference(terms_and_conditions);


        RelativeLayout layout = new RelativeLayout(this);
        layout.setId(1100);
        AbsListView.LayoutParams p = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,AbsListView.LayoutParams.WRAP_CONTENT);
        layout.setLayoutParams(p);

        TextView log_out_text = new TextView(this);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
        log_out_text.setTextSize(TypedValue.COMPLEX_UNIT_SP,18);
        log_out_text.setTypeface(null,Typeface.BOLD);
        log_out_text.setGravity(Gravity.CENTER);
        log_out_text.setTextColor(Color.parseColor("#FFFFFF"));
        log_out_text.setBackgroundDrawable(log_out_drawable);
        log_out_text.setLayoutParams(params);
        layout.addView(log_out_text);

        Preference log_out = new Preference(this);
        log_out.setLayoutResource(1100);
        root.addPreference(log_out);

        setPreferenceScreen(root);

问题是从上面的第三行……任何人都可以帮助我????

错误是:

01-30 10:56:16.886: E/AndroidRuntime(3420): FATAL EXCEPTION: main
01-30 10:56:16.886: E/AndroidRuntime(3420): android.content.res.Resources$NotFoundException: Resource ID #0x44c
01-30 10:56:16.886: E/AndroidRuntime(3420):     at android.content.res.Resources.getValue(Resources.java:892)
01-30 10:56:16.886: E/AndroidRuntime(3420):     at android.content.res.Resources.loadXmlResourceParser(Resources.java:1869)
01-30 10:56:16.886: E/AndroidRuntime(3420):     at android.content.res.Resources.getLayout(Resources.java:731)
01-30 10:56:16.886: E/AndroidRuntime(3420):     at android.view.LayoutInflater.inflate(LayoutInflater.java:318)
01-30 10:56:16.886: E/AndroidRuntime(3420):     at android.preference.Preference.onCreateView(Preference.java:412)
01-30 10:56:16.886: E/AndroidRuntime(3420):     at android.preference.Preference.getView(Preference.java:389)
01-30 10:56:16.886: E/AndroidRuntime(3420):     at android.preference.PreferenceGroupAdapter.getView(PreferenceGroupAdapter.java:221)
01-30 10:56:16.886: E/AndroidRuntime(3420):     at android.widget.AbsListView.obtainView(AbsListView.java:1554)
01-30 10:56:16.886: E/AndroidRuntime(3420):     at android.widget.ListView.makeAndAddView(ListView.java:1793)
01-30 10:56:16.886: E/AndroidRuntime(3420):     at android.widget.ListView.fillDown(ListView.java:718)
01-30 10:56:16.886: E/AndroidRuntime(3420):     at android.widget.ListView.fillFromTop(ListView.java:775)
01-30 10:56:16.886: E/AndroidRuntime(3420):     at android.widget.ListView.layoutChildren(ListView.java:1646)
01-30 10:56:16.886: E/AndroidRuntime(3420):     at android.widget.AbsListView.onLayout(AbsListView.java:1384)
01-30 10:56:16.886: E/AndroidRuntime(3420):     at android.view.View.layout(View.java:7228)
01-30 10:56:16.886: E/AndroidRuntime(3420):     at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
01-30 10:56:16.886: E/AndroidRuntime(3420):     at android.view.View.layout(View.java:7228)
01-30 10:56:16.886: E/AndroidRuntime(3420):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
01-30 10:56:16.886: E/AndroidRuntime(3420):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
01-30 10:56:16.886: E/AndroidRuntime(3420):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
01-30 10:56:16.886: E/AndroidRuntime(3420):     at android.view.View.layout(View.java:7228)
01-30 10:56:16.886: E/AndroidRuntime(3420):     at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
01-30 10:56:16.886: E/AndroidRuntime(3420):     at android.view.View.layout(View.java:7228)
01-30 10:56:16.886: E/AndroidRuntime(3420):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1145)
01-30 10:56:16.886: E/AndroidRuntime(3420):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1865)
01-30 10:56:16.886: E/AndroidRuntime(3420):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-30 10:56:16.886: E/AndroidRuntime(3420):     at android.os.Looper.loop(Looper.java:130)
01-30 10:56:16.886: E/AndroidRuntime(3420):     at android.app.ActivityThread.main(ActivityThread.java:3687)
01-30 10:56:16.886: E/AndroidRuntime(3420):     at java.lang.reflect.Method.invokeNative(Native Method)
01-30 10:56:16.886: E/AndroidRuntime(3420):     at java.lang.reflect.Method.invoke(Method.java:507)
01-30 10:56:16.886: E/AndroidRuntime(3420):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
01-30 10:56:16.886: E/AndroidRuntime(3420):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
01-30 10:56:16.886: E/AndroidRuntime(3420):     at dalvik.system.NativeStart.main(Native Method)

解决方法

你要做的是:创建一个资源programaticaly.你不能这样做.在编译过程中会创建Android资源. (基本上xml文件由SDK处理/编译,这就是资源的创建方式).

您可以尝试的是:

>创建自定义公共类MyPreference扩展了Preference
>覆盖onCreateView(ViewGroup父级)以返回所需的布局.

并使用这样的类:

Preference log_out = new MyPreference(this);
    root.addPreference(log_out);

猜你在找的Android相关文章