5

I want to be able to store a font size (18sp) in a resource so that a lot of my layouts will use so that I can easily change the size in the future if I need to. I've a string in a string resource file with "18sp" as the value, and I can call it in the layout editor by setting the text size to "@strings/string_name". At that point everything is fine, and the text is changed to the correct font. The problem is once I try to test that on an actual device I get :

04-13 12:01:01.210: E/AndroidRuntime(17114): Caused by: android.view.InflateException:    Binary XML file line #50: Error inflating class <unknown>


04-13 12:01:01.210: E/AndroidRuntime(17114):    at 

android.view.LayoutInflater.createView(LayoutInflater.java:606)
04-13 12:01:01.210: E/AndroidRuntime(17114):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
04-13 12:01:01.210: E/AndroidRuntime(17114):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
04-13 12:01:01.210: E/AndroidRuntime(17114):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
04-13 12:01:01.210: E/AndroidRuntime(17114):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
04-13 12:01:01.210: E/AndroidRuntime(17114):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
04-13 12:01:01.210: E/AndroidRuntime(17114):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
04-13 12:01:01.210: E/AndroidRuntime(17114):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
04-13 12:01:01.210: E/AndroidRuntime(17114):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
04-13 12:01:01.210: E/AndroidRuntime(17114):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
04-13 12:01:01.210: E/AndroidRuntime(17114):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
04-13 12:01:01.210: E/AndroidRuntime(17114):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
04-13 12:01:01.210: E/AndroidRuntime(17114):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
04-13 12:01:01.210: E/AndroidRuntime(17114):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
04-13 12:01:01.210: E/AndroidRuntime(17114):    at android.app.Activity.setContentView(Activity.java:1835)
04-13 12:01:01.210: E/AndroidRuntime(17114):    
04-13 12:01:01.210: E/AndroidRuntime(17114):    at android.app.Activity.performCreate(Activity.java:4465)
04-13 12:01:01.210: E/AndroidRuntime(17114):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-13 12:01:01.210: E/AndroidRuntime(17114):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
04-13 12:01:01.210: E/AndroidRuntime(17114):    ... 11 more
04-13 12:01:01.210: E/AndroidRuntime(17114): Caused by: java.lang.reflect.InvocationTargetException
04-13 12:01:01.210: E/AndroidRuntime(17114):    at java.lang.reflect.Constructor.constructNative(Native Method)
04-13 12:01:01.210: E/AndroidRuntime(17114):    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
04-13 12:01:01.210: E/AndroidRuntime(17114):    at android.view.LayoutInflater.createView(LayoutInflater.java:586)
04-13 12:01:01.210: E/AndroidRuntime(17114):    ... 29 more
04-13 12:01:01.210: E/AndroidRuntime(17114): Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x3
04-13 12:01:01.210: E/AndroidRuntime(17114):    at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:463)
04-13 12:01:01.210: E/AndroidRuntime(17114):    at android.widget.TextView.<init>(TextView.java:786)
04-13 12:01:01.210: E/AndroidRuntime(17114):    at android.widget.TextView.<init>(TextView.java:442)

Is this because I am using a string resource as a text size, or would it be some other underlying problem? If it is because i am using a string resource, is there a different / more proper way to have a global text size that I can call on so that I can change everything by modifying 1 value?

Also to note: My app worked just fine before I tried using a string resource as a text size, so I'm relatively certain it can't be something unrelated that is causing the error.

THanks!

4

2 回答 2

10

在 res 目录中创建一个 dimens.xml 文件,其内容类似于:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="some_text_size">18dp</dimen>
</resources>

然后不要像@string/blah 那样引用它,而是使用@dimen/blah。

于 2012-04-13T18:13:36.363 回答
4

将其用作您的 XML 资源并从您想要的任何地方调用它...

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="textview_height">25dp</dimen>
    <dimen name="textview_width">150dp</dimen>
    <dimen name="ball_radius">30dp</dimen>
    <dimen name="font_size">16sp</dimen>
</resources>

然后在代码中调用它:

Resources res = getResources();
float fontSize = res.getDimension(R.dimen.font_size);
于 2012-04-13T18:15:05.720 回答