- Google Android — это несложно
- Создать массив со значениями из ресурсов
- Создать массив со значениями из ресурсов
- Re: Создать массив со значениями из ресурсов
- Re: Создать массив со значениями из ресурсов
- Re: Создать массив со значениями из ресурсов
- Re: Создать массив со значениями из ресурсов
- Re: Создать массив со значениями из ресурсов
- String resources
- String
- Kotlin
- String array
- Kotlin
- Quantity strings (plurals)
- Kotlin
- Format and style
- Handle special characters
- Formatting strings
- Kotlin
- Styling with HTML markup
- Kotlin
- Kotlin
- Styling with spannables
- Kotlin
- Kotlin
- Kotlin
- Styling with annotations
- Example — adding a custom typeface
- Kotlin
- Annotation spans and text parceling
- Kotlin
- Kotlin
Google Android — это несложно
Добро пожаловать на форум сайта
Создать массив со значениями из ресурсов
Создать массив со значениями из ресурсов
Сообщение grem17 » 03 фев 2015, 11:34
имеется большой список по типу «имя, значение», хочу создать его в ресурсах, например через string
Re: Создать массив со значениями из ресурсов
Сообщение grem17 » 03 фев 2015, 12:55
в итоге получаю строку
@string/Sm
@string/Sa
а как получить на которую ссылается например @string/Sm ?
или как из строки @string/Sm получить значение ресурса?
Re: Создать массив со значениями из ресурсов
Сообщение altwin » 03 фев 2015, 18:27
Re: Создать массив со значениями из ресурсов
Сообщение grem17 » 04 фев 2015, 08:24
Re: Создать массив со значениями из ресурсов
Сообщение grem17 » 04 фев 2015, 08:31
Re: Создать массив со значениями из ресурсов
Сообщение grem17 » 05 фев 2015, 12:00
еще вопрос по теме, вложенный массив имеет ссылки на графические ресурсы, которые я потом буду использовать в проге.
после обработки, получаю на выходе строковое значение String с вот таким значем \res\drawable\aa (это картинка), а как мне строку преобразовать в ресурс и использовать ее? или как хранить ссылку на ресурс, чтобы потом его можно было использовать по назначению?
Источник
String resources
A string resource provides text strings for your application with optional text styling and formatting. There are three types of resources that can provide your application with strings:
String XML resource that provides a single string. String Array XML resource that provides an array of strings. Quantity Strings (Plurals) XML resource that carries different strings for pluralization.
All strings are capable of applying some styling markup and formatting arguments. For information about styling and formatting strings, see the section about Formatting and Styling.
String
A single string that can be referenced from the application or from other resource files (such as an XML layout).
Note: A string is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file). So, you can combine string resources with other simple resources in the one XML file, under one element.
file location: res/values/filename.xml
The filename is arbitrary. The element’s name is used as the resource ID. compiled resource datatype: Resource pointer to a String . resource reference: In Java: R.string.string_name
In XML: @string/string_name syntax: elements: Required. This must be the root node.
A string, which can include styling tags. Beware that you must escape apostrophes and quotation marks. For more information about how to properly style and format your strings see Formatting and Styling, below.
name String. A name for the string. This name is used as the resource ID. example: XML file saved at res/values/strings.xml :
This layout XML applies a string to a View:
This application code retrieves a string:
Kotlin
You can use either getString(int) or getText(int) to retrieve a string. getText(int) retains any rich text styling applied to the string.
String array
An array of strings that can be referenced from the application.
Note: A string array is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file). As such, you can combine string array resources with other simple resources in the one XML file, under one element.
file location: res/values/filename.xml
The filename is arbitrary. The element’s name is used as the resource ID. compiled resource datatype: Resource pointer to an array of String s. resource reference: In Java: R.array.string_array_name
In XML: @[package:]array/string_array_name syntax: elements: Required. This must be the root node.
Defines an array of strings. Contains one or more elements.
name String. A name for the array. This name is used as the resource ID to reference the array. A string, which can include styling tags. The value can be a reference to another string resource. Must be a child of a element. Beware that you must escape apostrophes and quotation marks. See Formatting and Styling, below, for information about to properly style and format your strings.
example: XML file saved at res/values/strings.xml :
This application code retrieves a string array:
Kotlin
Quantity strings (plurals)
Different languages have different rules for grammatical agreement with quantity. In English, for example, the quantity 1 is a special case. We write «1 book», but for any other quantity we’d write «n books». This distinction between singular and plural is very common, but other languages make finer distinctions. The full set supported by Android is zero , one , two , few , many , and other .
The rules for deciding which case to use for a given language and quantity can be very complex, so Android provides you with methods such as getQuantityString() to select the appropriate resource for you.
Although historically called «quantity strings» (and still called that in API), quantity strings should only be used for plurals. It would be a mistake to use quantity strings to implement something like Gmail’s «Inbox» versus «Inbox (12)» when there are unread messages, for example. It might seem convenient to use quantity strings instead of an if statement, but it’s important to note that some languages (such as Chinese) don’t make these grammatical distinctions at all, so you’ll always get the other string.
The selection of which string to use is made solely based on grammatical necessity. In English, a string for zero is ignored even if the quantity is 0, because 0 isn’t grammatically different from 2, or any other number except 1 («zero books», «one book», «two books», and so on). Conversely, in Korean only the other string is ever used.
Don’t be misled either by the fact that, say, two sounds like it could only apply to the quantity 2: a language may require that 2, 12, 102 (and so on) are all treated like one another but differently to other quantities. Rely on your translator to know what distinctions their language actually insists upon.
It’s often possible to avoid quantity strings by using quantity-neutral formulations such as «Books: 1». This makes your life and your translators’ lives easier, if it’s an acceptable style for your application.
Note: A plurals collection is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file). As such, you can combine plurals resources with other simple resources in the one XML file, under one element.
file location: res/values/filename.xml
The filename is arbitrary. The
element’s name is used as the resource ID. resource reference: In Java: R.plurals.plural_name syntax: elements: Required. This must be the root node.
A collection of strings, of which, one string is provided depending on the amount of something. Contains one or more elements.
name String. A name for the pair of strings. This name is used as the resource ID. A plural or singular string. The value can be a reference to another string resource. Must be a child of a
element. Beware that you must escape apostrophes and quotation marks. See Formatting and Styling, below, for information about to properly style and format your strings.
quantity Keyword. A value indicating when this string should be used. Valid values, with non-exhaustive examples in parentheses:
Value | Description |
---|---|
zero | When the language requires special treatment of the number 0 (as in Arabic). |
one | When the language requires special treatment of numbers like one (as with the number 1 in English and most other languages; in Russian, any number ending in 1 but not ending in 11 is in this class). |
two | When the language requires special treatment of numbers like two (as with 2 in Welsh, or 102 in Slovenian). |
few | When the language requires special treatment of «small» numbers (as with 2, 3, and 4 in Czech; or numbers ending 2, 3, or 4 but not 12, 13, or 14 in Polish). |
many | When the language requires special treatment of «large» numbers (as with numbers ending 11-99 in Maltese). |
other | When the language does not require special treatment of the given quantity (as with all numbers in Chinese, or 42 in English). |
example: XML file saved at res/values/strings.xml :
XML file saved at res/values-pl/strings.xml :
Kotlin
When using the getQuantityString() method, you need to pass the count twice if your string includes string formatting with a number. For example, for the string %d songs found , the first count parameter selects the appropriate plural string and the second count parameter is inserted into the %d placeholder. If your plural strings do not include string formatting, you don’t need to pass the third parameter to getQuantityString .
Format and style
Here are a few important things you should know about how to properly format and style your string resources.
Handle special characters
When a string contains characters that have special usage in XML, you must escape the characters according to the standard XML/HTML escaping rules. If you need to escape a character that has special meaning in Android you should use a preceding backslash.
By default Android will collapse sequences of whitespace characters into a single space. You can avoid this by enclosing the relevant part of your string in double quotes. In this case all whitespace characters (including new lines) will get preserved within the quoted region. Double quotes will allow you to use regular single unescaped quotes as well.
Any of the following:
- \’
- Enclose the entire string in double quotes ( «This’ll work» , for example)
Character | Escaped form(s) |
---|---|
@ | \@ |
? | \? |
New line | \n |
Tab | \t |
U+XXXX Unicode character | \uXXXX |
Single quote ( ‘ ) | |
Double quote ( » ) | \» Note that surrounding the string with single quotes does not work. Whitespace collapsing and Android escaping happens after your resource file gets parsed as XML. This means that (space, punctuation space, Unicode Em space) all collapse to a single space ( » » ), because they are all Unicode spaces after the file is parsed as an XML. To preserve those spaces as they are, you can either quote them ( » » ) or use Android escaping ( \u0032 \u8200 \u8195 ). Note: From XML parser’s perspective, there is no difference between «Test this» and «Test this» whatsoever. Both forms will not show any quotes but trigger Android whitespace-preserving quoting (that will have no practical effect in this case). Formatting stringsIf you need to format your strings, then you can do so by putting your format arguments in the string resource, as demonstrated by the following example resource. In this example, the format string has two arguments: %1$s is a string and %2$d is a decimal number. Then, format the string by calling getString(int, Object. ) . For example: KotlinStyling with HTML markupYou can add styling to your strings with HTML markup. For example: The following HTML elements are supported:
|