使用字符串资源的布局文字大小的Android日食 [英] android eclipse using string resource for text size in layouts
问题描述
我希望能够存储一个字体大小(18sp)的资源,使很多我的布局将使用,这样我可以很容易地改变大小,将来如果我需要。我有一个字符串与18sp作为值的字符串资源文件,我可以通过设置文字大小为@字符串/ string_name称呼它在布局编辑器。在这一点上一切都很好,而该文本被改变成正确的字体。现在的问题是,一旦我尝试测试,实际的设备我得到的:
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)
这是因为我使用字符串资源作为文本的大小,或者会是其他一些潜在的问题?如果是因为我使用的是字符串资源,是有不同的/更恰当的方式,有一个全球性的文本大小,我可以调用,这样我可以通过修改1值改变一切?
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.
谢谢!
推荐答案
与此类似内容的创建你的资源目录中的dimens.xml文件:
Create a dimens.xml file in your res directory with contents similar to this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="some_text_size">18dp</dimen>
</resources>
而不是引用它像@字符串/等等
然后,使用@扪/等等。
Then instead of referencing it like @string/blah, use @dimen/blah.
这篇关于使用字符串资源的布局文字大小的Android日食的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!