Android 访问 xml 中的整数资源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10230026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:56:13  来源:igfitidea点击:

Accessing integer resources in xml

androidandroid-layoutandroid-resources

提问by Rafiq

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

我读过这篇文章,发现如何访问 java 类中的整数资源,但没有其他资源的文档。

Here is Resources at res/values/integers.xml

这是 res/values/integers.xml 中的资源

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

Tried to access the input_field_padding in EditText.

试图访问 EditText 中的 input_field_padding。

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

是否可以在另一个资源文件中访问它,或者我遗漏了什么?

回答by Rafiq

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

最后,我能够访问 java 代码中的整数资源和 xml 中作为 dimen 的整数值。

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

In xml file:-

在 xml 文件中:-

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

In java file:-

在java文件中:-

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);

回答by Jay Soyer

The accepted answer is completely mis-guiding. Unless there is a specific unique reason, you should never use an Integer resource for setting padding sizes. Not only in XML but also in code. This is why you experienced an UnsupportedOperationException. Integer resources are not scaled based on the DPI of screens. Which means you will not get consistently spaced padding for all devices. Dimen resources will automatically adjust their value for you. Your java code should look like this:

接受的答案完全是误导。除非有特定的独特原因,否则不应使用 Integer 资源来设置填充大小。不仅在 XML 中,而且在代码中。这就是为什么你经历了一个UnsupportedOperationException. 整数资源不会根据屏幕的 DPI 进行缩放。这意味着您不会为所有设备获得一致的间隔填充。Dimen 资源会自动为您调整其价值。您的 Java 代码应如下所示:

EditText mUsername = (EditText) this.findViewById(R.id.user_name);
int padding = (int) this.getResources().getDimension(R.dimen.input_field_padding);
mUsername.setPadding(padding, padding, padding, padding);

Which, btw, there's no need to set the padding of the EditTextelement in code if you already set it in the XML. Unless you wanted to change it to a different value at run time.

顺便说一句,EditText如果您已经在 XML 中设置了元素的填充,则无需在代码中设置元素的填充。除非您想在运行时将其更改为不同的值。

Read more here:
Density Independence
Working with XML Layouts

在此处阅读更多信息:使用 XML 布局的
密度独立性

回答by Arash

Also You Can Define It This Way :

你也可以这样定义:

<resources>
<item name="text_line_spacing" format="integer" type="dimen">10</item>
</resources>

Format = enclosing data type:

格式 = 封闭数据类型:

float boolean fraction integer ... and type stands for:

浮点布尔分数整数...和类型代表:

Type = resource type (referenced with R.XXXXX.name):

Type = 资源类型(以 R.XXXXX.name 引用):

color dimen string style etc...

颜色 dimen 字符串样式等...

Access It Like This :

像这样访问它:

Resources res = getResources();
int i= res.getInteger(R.dimen.int_value);

回答by Sydroid

if you are taking Integer resource then you have to take R.Integer.yourintvalue like this way

如果您正在使用 Integer 资源,那么您必须像这样使用 R.Integer.yourintvalue

int value=(int) contextG.getResources()
            .getDimension(R.integer.myvalue);

回答by 100rabh

The correct way to access an integer field is using @integer/input_field_padding

访问整数字段的正确方法是使用 @integer/input_field_padding

In XML

在 XML 中

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

In JAVA

在JAVA中

R.integer.integer_name

Refer this http://developer.android.com/guide/topics/resources/more-resources.html#Integer.

请参阅此http://developer.android.com/guide/topics/resources/more-resources.html#Integer

回答by Hemant Sharma

 <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"

android:text="@integer/imageSelectLimit"

android:text="@integer/imageSelectLimit"

       android:textColor="@color/colorPrimary"
       android:textSize="@dimen/textSize"/>