我想R.drawable.*使用XML值文件以数组内部的形式存储可绘制资源的ID ,然后在我的活动中检索数组.
有关如何实现这一目标的任何想法?
arrays android android-xml android-resources android-drawable
我只是想从我的数组中显示一个列表arrays.xml.当我尝试在模拟器中运行它时,我收到强制关闭消息.
如果我在java文件中定义数组
String[] testArray = new String[] {"one","two","three","etc"};
Run Code Online (Sandbox Code Playgroud)
它有效,但是当我使用时
String[] testArray = getResources().getStringArray(R.array.testArray);
Run Code Online (Sandbox Code Playgroud)
它不起作用.
这是我的Java文件:
package com.xtensivearts.episode.seven;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class Episode7 extends ListActivity {
String[] testArray = getResources().getStringArray(R.array.testArray);
/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create an ArrayAdapter that will contain all list items
ArrayAdapter<String> adapter;
/* Assign the name array to that adapter and
also choose a simple layout for the list …Run Code Online (Sandbox Code Playgroud) 如何在array.xml中保存颜色值并将其作为Color []数组检索回我的代码?
先谢谢!
我使用复数来编译Android应用程序的数量字符串.我完全按照教程中的内容进行操作:
res.getQuantityString(
R.plurals.number_of_comments, commentsCount, commentsCount);
Run Code Online (Sandbox Code Playgroud)
这是复数的定义:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<plurals name="number_of_comments">
<item quantity="zero">No comments</item>
<item quantity="one">One comment</item>
<item quantity="other">%d comments</item>
</plurals>
</resources>
Run Code Online (Sandbox Code Playgroud)
有趣的是,输出字符串与我所定义的一样奇怪:
commentsCount = 0 => "0 comments"
commentsCount = 1 => "One comment"
commentsCount = 2 => "2 comments"
Run Code Online (Sandbox Code Playgroud)
我想这是因为文档说明When the language requires special treatment of the number 0 (as in Arabic).了zero数量.有没有办法强迫我的定义?