Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android xml中整数资源的访问_Android_Android Layout_Android Resources - Fatal编程技术网

Android xml中整数资源的访问

Android xml中整数资源的访问,android,android-layout,android-resources,Android,Android Layout,Android Resources,我读过where,它发现了如何访问java类中的整数资源,但没有其他资源的文档 以下是res/values/integers.xml上的参考资料 <resources> <integer name="input_field_padding" >5</integer> </resources> 是否有可能在另一个资源文件中访问它,或者我遗漏了什么?最后,我能够访问java代码中的整数资源和xml中作为dimen的整数值 <?xml

我读过where,它发现了如何访问java类中的整数资源,但没有其他资源的文档

以下是res/values/integers.xml上的参考资料

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

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

最后,我能够访问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>

公认的答案完全是错误的。除非有特定的唯一原因,否则永远不要使用整数资源来设置填充大小。不仅在XML中,而且在代码中。这就是您遇到
不支持操作异常的原因。整数资源不会根据屏幕的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);
顺便说一句,如果您已经在XML中设置了
EditText
元素,则无需在代码中设置该元素的填充。除非您想在运行时将其更改为其他值

请在此处阅读更多信息:


如果要获取整数资源,则必须以这种方式获取R.Integer.yourintvalue

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

您也可以这样定义它:

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

访问整数字段的正确方法是使用
@integer/input\u field\u padding

在XML中

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

参考这个

这个答案是错误的。根据用例的不同,这是错误的。请看下面我的答案。你已经创建了整数,它不是非法的,但是是非标准的,但是你从来没有使用过它。您只使用了创建的与整数同名的dimen。至少这是一种不好的做法。@DavidMays是的,被评论的行不是非法的,它对我有用。根据你的建议,我对它进行了更新。你必须在XML中使用它,比如:@integer/input\u field\u padding。我很惊讶为什么有这么多人对此进行投票,事实上,你使用的是dimen而不是integer。你的第二行不应该是
R.dimen.input\u field\u padding
?哎呀。当我复制/粘贴时,错过了调整。塔克斯!这使得填充比直接在xml中创建视图时更小。为什么?我可以使用我想要的任何类型,或者有一组可能的字符串吗?颜色、尺寸、字符串、样式等等。这是正确的方法。作为参考,我的
integer\u name
值在
values/integers.xml
中是这样的:
42
直接使用
R.integer.integer\u name
是错误的!它只保存整数的
资源id
。您应该使用
getResources().getInteger(R.integer.SPLASH\u SCREEN\u DELAY)
,如Arash的回答()
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);
int value=(int) contextG.getResources()
            .getDimension(R.integer.myvalue);
<resources>
<item name="text_line_spacing" format="integer" type="dimen">10</item>
</resources>
Resources res = getResources();
int i= res.getInteger(R.dimen.int_value);
 <EditText
        android:id="@+id/user_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"    
        android:padding="@dimen/input_field_padding" /> 
R.integer.integer_name
 <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:textColor="@color/colorPrimary"
       android:textSize="@dimen/textSize"/>