访问整数资源XML整数、资源、XML

由网友(盯着贞子唱咆哮)分享简介:我已经阅读这在那里发现,如何获得整数资源Java类,但没有文档的其他资源。I have read this where found that how to access integer resources in java class but no docs for another resource.下面是在资源RE...

我已经阅读这在那里发现,如何获得整数资源Java类,但没有文档的其他资源。

I have read this where found that how to access integer resources in java class but no docs for another resource.

下面是在资源RES /价值/ integers.xml

Here is Resources at res/values/integers.xml

<resources>
     <integer name="input_field_padding" >5</integer>
</resources>

试图访问input_field_padding中的EditText。

Tried to access the input_field_padding in EditText.

<EditText
        android:id="@+id/user_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"    
        android:padding="@integer/input_field_padding" />

不过,我得到了以下异常

But I got the following exception

java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x10

是否有可能访问在另一个资源文件还是我失去了一些东西?

Is it possible to access it in another resources file or am I missing something?

推荐答案

最后我能够访问整数资源在java中code和整数值作为XML扪。

Finally I am able to access integer resource in java code and integer value as dimen in xml.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="input_field_padding">20dip</dimen>
    <integer name="input_field_padding">20</integer>
</resources>

在XML文件: -

In xml file:-

<EditText
    android:id="@+id/user_name"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"    
    android:padding="@dimen/input_field_padding" /> 

在Java文件中: -

In java file:-

EditText mUsername = (EditText) this.findViewById(R.id.user_name);
//int padding = this.getResources().getInteger(R.integer.input_field_padding);
int padding = (int) this.getResources().getDimension(R.dimen.input_field_padding);
mUsername.setPadding(padding, padding, padding, padding);
阅读全文

相关推荐

最新文章