0

我想让用户可以通过首选项中的 ListPreference 在我的应用程序中更改我的文本的 textSize。我怎样才能做到?

它尝试了一个字符串变量,但是当我写例如 25sp 时出现了一个异常 .. 那里有什么问题?

这是一个例外:

04-07 09:02:20.475: ERROR/AndroidRuntime(287): android.view.InflateException: Binary XML file line #19: Error inflating class <unknown>
04-07 09:02:20.475: ERROR/AndroidRuntime(287):     at android.view.LayoutInflater.createView(LayoutInflater.java:513)
04-07 09:02:20.475: ERROR/AndroidRuntime(287):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
04-07 09:02:20.475: ERROR/AndroidRuntime(287):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
04-07 09:02:20.475: ERROR/AndroidRuntime(287):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
04-07 09:02:20.475: ERROR/AndroidRuntime(287):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
04-07 09:02:20.475: ERROR/AndroidRuntime(287):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
04-07 09:02:20.475: ERROR/AndroidRuntime(287):     at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
04-07 09:02:20.475: ERROR/AndroidRuntime(287):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
04-07 09:02:20.475: ERROR/AndroidRuntime(287):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
04-07 09:02:20.475: ERROR/AndroidRuntime(287):     at de.mprengemann.hwr.timetabel.SubjectListView$SubjectAdapter.getView(SubjectListView.java:881)
04-07 09:02:20.475: ERROR/AndroidRuntime(287):     at android.widget.AbsListView.obtainView(AbsListView.java:1315)
04-07 09:02:20.475: ERROR/AndroidRuntime(287):     at android.widget.ListView.makeAndAddView(ListView.java:1727)
04-07 09:02:20.475: ERROR/AndroidRuntime(287):     at android.widget.ListView.fillDown(ListView.java:652)
04-07 09:02:20.475: ERROR/AndroidRuntime(287):     at android.widget.ListView.fillFromTop(ListView.java:709)
04-07 09:02:20.475: ERROR/AndroidRuntime(287):     at android.widget.ListView.layoutChildren(ListView.java:1580)
04-07 09:02:20.475: ERROR/AndroidRuntime(287):     at android.widget.AbsListView.onLayout(AbsListView.java:1147)
04-07 09:02:20.475: ERROR/AndroidRuntime(287):     at android.view.View.layout(View.java:7035)
04-07 09:02:20.475: ERROR/AndroidRuntime(287):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
04-07 09:02:20.475: ERROR/AndroidRuntime(287):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
04-07 09:02:20.475: ERROR/AndroidRuntime(287):     at android.widget.TableLayout.onLayout(TableLayout.java:437)
04-07 09:02:20.475: ERROR/AndroidRuntime(287):     at android.view.View.layout(View.java:7035)
04-07 09:02:20.475: ERROR/AndroidRuntime(287):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
04-07 09:02:20.475: ERROR/AndroidRuntime(287):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
04-07 09:02:20.475: ERROR/AndroidRuntime(287):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
04-07 09:02:20.475: ERROR/AndroidRuntime(287):     at android.view.View.layout(View.java:7035)
04-07 09:02:20.475: ERROR/AndroidRuntime(287):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
04-07 09:02:20.475: ERROR/AndroidRuntime(287):     at android.view.View.layout(View.java:7035)
04-07 09:02:20.475: ERROR/AndroidRuntime(287):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
04-07 09:02:20.475: ERROR/AndroidRuntime(287):     at android.view.View.layout(View.java:7035)
04-07 09:02:20.475: ERROR/AndroidRuntime(287):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
04-07 09:02:20.475: ERROR/AndroidRuntime(287):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
04-07 09:02:20.475: ERROR/AndroidRuntime(287):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-07 09:02:20.475: ERROR/AndroidRuntime(287):     at android.os.Looper.loop(Looper.java:123)
04-07 09:02:20.475: ERROR/AndroidRuntime(287):     at android.app.ActivityThread.main(ActivityThread.java:4627)
04-07 09:02:20.475: ERROR/AndroidRuntime(287):     at java.lang.reflect.Method.invokeNative(Native Method)
04-07 09:02:20.475: ERROR/AndroidRuntime(287):     at java.lang.reflect.Method.invoke(Method.java:521)
04-07 09:02:20.475: ERROR/AndroidRuntime(287):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-07 09:02:20.475: ERROR/AndroidRuntime(287):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-07 09:02:20.475: ERROR/AndroidRuntime(287):     at dalvik.system.NativeStart.main(Native Method)
04-07 09:02:20.475: ERROR/AndroidRuntime(287): Caused by: java.lang.reflect.InvocationTargetException
04-07 09:02:20.475: ERROR/AndroidRuntime(287):     at android.widget.TextView.<init>(TextView.java:321)
04-07 09:02:20.475: ERROR/AndroidRuntime(287):     at java.lang.reflect.Constructor.constructNative(Native Method)
04-07 09:02:20.475: ERROR/AndroidRuntime(287):     at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
04-07 09:02:20.475: ERROR/AndroidRuntime(287):     at android.view.LayoutInflater.createView(LayoutInflater.java:500)
04-07 09:02:20.475: ERROR/AndroidRuntime(287):     ... 38 more
04-07 09:02:20.475: ERROR/AndroidRuntime(287): Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x3
04-07 09:02:20.475: ERROR/AndroidRuntime(287):     at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:463)
04-07 09:02:20.475: ERROR/AndroidRuntime(287):     at android.widget.TextView.<init>(TextView.java:640)
04-07 09:02:20.475: ERROR/AndroidRuntime(287):     ... 42 more

非常感谢 :)

4

1 回答 1

0

我不确定我是否正确理解了您的问题,但是如果您尝试在 ListPreference 中显示多个大小选项,则必须将条目添加为数组。

这是一个例子。第三行设置一个预选值:

 ListPreference fontSize = new ListPreference(this);
 fontSize.setEntries(R.array.font_sizes_displayed);
 fontSize.setEntryValues(R.array.font_sizes);
 fontSize.setValue("12");

您需要在 res\values 中创建一个数组文件“array.xml”:

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <string-array name="font_sizes_displayed">
        <item>10 pt</item>
        <item>12 pt</item>
        <item>14 pt</item>
    </string-array>
    <string-array name="font_sizes">
        <item>10</item>
        <item>12</item>
        <item>14</item>
    </string-array>
</resources>

当然,您可以两次使用同一个数组。您也可以以编程方式编写它:

proxyType.setEntries(new String[]{"10", "12", "14"});
proxyType.setEntryValues(new String[]{"10", "12", "14"});

要在代码的其他地方访问它,您可以通过键找到首选项,并获取值,如下所示:

ListPreference fontSizePref = (ListPreference) findPreference("preferenceKey");
fontSizePref.getValue();
于 2011-04-15T18:44:25.637 回答