如何解析一维字符串并把它转换为一个尺寸值 [英] How to parse a dimension string and convert it to a dimension value
问题描述
我在寻找一种方式来动态地转换如30dp
字符串到类似的像素数的int。这意味着 StaticClass.theMethodImSearchingFor(16像素)
将返回 16
。
我的应用程序将动态地获得这些字符串,我需要一种方法来存储它作为一个像素值以后使用。
我已经看了<一href="http://www.java2s.com/Open-Source/Android/android-core/platform-frameworks-base/Catalogplatform-frameworks-base.htm">Android来源$ C $ C ,主要是类资源
, TypedArray
和的TypedValue
,但我找不到任何有用的东西。
I'm looking for a way to dynamically convert a String like "30dp"
into an int that resembles the amount of pixels. This would mean that StaticClass.theMethodImSearchingFor("16px")
would return 16
.
My application will get these Strings dynamically and I need a way to store it as a pixel value to use later.
I've already looked at the Android Source code, mainly the classes Resources
, TypedArray
and TypedValue
, but I couldn't find anything useful.
推荐答案
如果您需要在Android资源扪作为INT,你可以在你的code做到这一点:
If you need the android resource dimen as a int you can do this in your code:
context.getResources().getDimensionPixelSize(R.dimen.your_dimen_res);
这篇关于如何解析一维字符串并把它转换为一个尺寸值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!