简体   繁体   中英

How can I change the textSize in my theme?

I want to give the user the possibility to change the textSize of my text in my app by a ListPreference in the preferences. How can I make it?

It tried it with a string-variable but there was an exception when I wrote eg 25sp .. What's the problem there?

Here is the exception:

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

Thanks a lot :)

I'm not sure if I understand your problem correcly, but if you are trying to present several size options in the ListPreference, you have to add the entries as an array.

Here is an example. The third line sets a pre-selected value:

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

You need to create an array file "array.xml" in res\\values:

<?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>

Of course, you can use the same array twice. You can also write it programatically:

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

And to access it elsewhere in your code, you find the preference by the key, and get the value, like so:

ListPreference fontSizePref = (ListPreference) findPreference("preferenceKey");
fontSizePref.getValue();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM