如何更改主题中的文字大小?

时间:2011-04-06 18:43:36

标签: android android-layout android-preferences text-size

我想让用户可以通过首选项中的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

非常感谢:)

1 个答案:

答案 0 :(得分: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();