Przechowywanie identyfikatorów R. drawable w tablicy XML
Chciałbym zapisać identyfikator drawable resources w postaci R.drawable.*
wewnątrz tablicy używając pliku wartości XML, a następnie pobrać tablicę w mojej aktywności.
4 answers
Używasz wpisanej tablicy W arrays.xml
pliku w folderze /res
, który wygląda tak:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer-array name="random_imgs">
<item>@drawable/car_01</item>
<item>@drawable/balloon_random_02</item>
<item>@drawable/dog_03</item>
</integer-array>
</resources>
Następnie w swojej aktywności, dostęp do nich w następujący sposób:
TypedArray imgs = getResources().obtainTypedArray(R.array.random_imgs);
// get resource ID by index
imgs.getResourceId(i, -1)
// or set you ImageView's resource to the id
mImgView1.setImageResource(imgs.getResourceId(i, -1));
// recycle the array
imgs.recycle();
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-30 15:17:08
W folderze value
Utwórz {[4] } nazwę pliku arrays.xml
Dodaj do niego dane w ten sposób
<integer-array name="your_array_name">
<item>@drawable/1</item>
<item>@drawable/2</item>
<item>@drawable/3</item>
<item>@drawable/4</item>
</integer-array>
Następnie Uzyskaj go do swojego kodu w ten sposób
private TypedArray img;
img = getResources().obtainTypedArray(R.array.your_array_name);
Następnie użyć Drawable
tych w img
TypedArray
na przykład jako ImageView
background
Użyj następującego kodu
ImageView.setBackgroundResource(img.getResourceId(index, defaultValue));
Gdzie index
jest indeksem Drawable
.
defaultValue
jest wartością, którą podajesz, jeśli nie ma elementu w tym index
Aby uzyskać więcej informacji o TypedArray
odwiedź ten link
http://developer.android.com/reference/android/content/res/TypedArray.html
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-02-15 20:48:17
Możesz użyć tego do stworzenia tablicy innych zasobów, takich jak drawables. Zauważ, że tablica nie musi być jednorodna, więc możesz utworzyć tablicę mieszanych typów zasobów, ale musisz być świadomy tego, co i gdzie są typy danych w tablicy.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="icons">
<item>@drawable/home</item>
<item>@drawable/settings</item>
<item>@drawable/logout</item>
</array>
<array name="colors">
<item>#FFFF0000</item>
<item>#FF00FF00</item>
<item>#FF0000FF</item>
</array>
</resources>
I zdobądź zasoby w swojej aktywności w ten sposób
Resources res = getResources();
TypedArray icons = res.obtainTypedArray(R.array.icons);
Drawable drawable = icons.getDrawable(0);
TypedArray colors = res.obtainTypedArray(R.array.colors);
int color = colors.getColor(0,0);
Smacznego!!!!!Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-07-07 16:24:03
Nie można przechowywać tablic w R. z tego co wiem.
Możesz utworzyć tablicę w config.xml lub strings.xml, który mapuje ścieżkę do zasobu rysowalnego za pomocą aliasu zasobu .
Sprawdź, czy to działa, i daj mi znać, jeśli potrzebujesz dodatkowej pomocy.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-08-04 17:20:28