Simple Values
Simple resource values는 resource의 타입이 명확한 포맷을 사용하며, string 처럼 표현된다. 따라서 표준 resource(/res/values/)로 정의되고, style 및 theme에 매핑한다거나, layout 과 같이 XML의 attribute로 사용되기도 한다.
Simple values에는 다음과 같은 종류가 있다.
1. Color Values
Alpha Channel을 가진 RGB를 사용한다.
Color Value는 #으로 시작하고 다음 포맷 중의 하나를 사용한다. (#RGB, #ARGB, #RRGGBB, #AARRGGBB)
Location: res/values/colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="opaque_red">#f00</color>
<color name="translucent_red">#80ff0000</color>
</resources>
Source Code에서 사용하기
// Retrieve a color value.
int color = getResources.getColor(R.color.opaque_red);
XML에서 사용하기
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAlign="center"
android:textColor="@color/translucent_red"
android:text="Some Text"/>
2. Strings
String도 Resource로 저장되고 조회될 수 씨는데 <b>, <i>, <u> 와 같은 표준 HTML Tag를 사용하여 정의할 수도 있다.
Location: res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="simple_welcome_message">Welcome!</string>
<string name="styled_welcome_message">We are <b><i>so</i></b> glad to see you.</string>
</resources>
Source Code에서 사용하기
// Assign a styled string resource to a TextView
// on the current screen.
CharSequence str = getString(R.string.styled_welcome_message);
TextView tv = (TextView)findViewByID(R.id.text);
tv.setText(str);
XML에서 사용하기
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAlign="center"
android:text="@string/simple_welcome_message"/>
Note: quote나 apostrophe를 사용하는 경우 escape 사용하거나 다른 quote로 전체 string을 닫아주어야 한다.
<string name="good_example">"This'll work"</string>
<string name="good_example_2">This\'ll also work</string>
3. Styled Text as a Format Strings
Styled Text을 사용하는 방법은 escaped HTML tag로 style tag를 저장한 후 formating 루 다시 styled text로 변경하는 방법을 사용하면 된다.
1) escaped string으로 styled text resource를 저장한다.
<resources>
<string name="search_results_resultsTextFormat">%1$d results for <b>&quot;%2$s&quot;</b></string>
</resources>
2) htmlEncode() method를 통해 escaped 되었는지 확인한다.
String escapedTitle = TextUtil.htmlEncode(title);
3) HTML Text를 formattig하기 위해 String.format() method를 사용한다. 그 후 HTML을 styled text로 변환하기 위해 fromHTML() method를 사용한다.
String resultsTextFormat = getContext().getResources().getString(R.string.search_results_resultsTextFormat);
String resultsText = String.format(resultsTextFormat, count, escapedTitle);
CharSequence styledResults = Html.fromHtml(resultsText);
4. Dimensions
XML에 dimension value를 정의함으로써 다양한 screen elements에서 사용되는 dimension을 생성할 수 있다.
안드로이드에서 지원하는 측정 단위는 다음과 같다.
px(Pixels): 실제 screen pixel
in(Inches): screen의 물리적 사이즈 기준
mm(Milimeters): screen의 물리적 사이즈 기준
pt(Points): screen의 물리적 사이즈에 기준한 inch의 1/72
dp(Density-independent Pixels): screen의 물리적 밀도에 기준한 추상적 단위.
sp(Scale-independent Pixels): dp와 유사하지만 사용자 폰트 사이즈 설정에 따른다.
Dimension value는 일반적으로 raw resource에서 사용되지 않고 XML의 attribute로서 사용된다.
Location: res/values/dimens.xml
<resources>
<dimen name="one_pixel">1px</dimen>
<dimen name="double_density">2dp</dimen>
<dimen name="sixteen_sp">16sp</dimen>
</resources>
Source Code에서 사용하기
float dimen = Resources.getDimen(R.dimen.one_pixel);
XML에서 사용하기
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="@dimen/sixteen_sp"/>
Drawables
Drawable 은 screen에 draw하기 위해 사용되는 resource type이다.
1. Bitmap Files
안드로이드는 약간 다른 포맷인 png, jpg, gif 포맷으로 bitmap resource file을 지원한다.bitmap file은 그 자체가 compile되고 확장자 없이 파일명으로만 참조된다.
Location: /res/drawable/my_image.png
Source Code에서 사용하기
Resources res = mContext.getResources();
Drawable myImage = res.getDrawable(R.drawable.my_image);
XML에서 사용하기
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tint="#55ff0000"
android:src="@drawable/my_image"/>
2. Color Drawables
color를 가진 사각형인 PaintDrawable object를 생성할 수도 있다.
Location: res/values/colors.xml
<resources>
<drawable name="solid_red">#f00</drawable>
<drawable name="solid_blue">#0000ff</drawable>
<drawable name="solid_green">#f0f0</drawable>
</resources>
Source Code 에서 사용하기
// Assign a PaintDrawable as the background to
// a TextView on the current screen.
Drawable redDrawable = Resources.getDrawable(R.drawable.solid_red);
TextView tv = (TextView)findViewByID(R.id.text);
tv.setBackground(redDrawable);
XML에서 사용하기
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAlign="center"
android:background="@drawable/solid_red"/>
3. Nine-Patch (stretchable) Images
안드로이드는 NinePatch graphic이라고 부르는 stretchable bitmap image를 지원한다. Stretchable Image란 stretchable section에 정의하는 PNG Image를 말한다. Stretchable Section은, text String 처럼 출력시 object를 가변 사이즈에 수용하지 위해 안드로이드가 resize하는 공간을 말한다.(?)
Location: res/drawable/some_name.9.png
Tip: 반드시 파일이름은 xxxx.9.png 로 끝나야 한다.
XML에서 사용하기
<Button id="@+id/tiny"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:text="Tiny"
android:textSize="8sp"
android:background="@drawable/my_button_background"/>
<Button id="@+id/big"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:text="Biiiiiiig text!"
android:textSize="30sp"
android:background="@drawable/my_button_background"/>
Animation
안드로이드는 하나의 graphic 또는 일련의 graphics 위에 간단한 anumation을 실행할 수 있으며, rotation, fading, moving 그리고 stretching을 포함한다.
Location: res/anim/some_file.xml
<set android:shareInterpolator=boolean> // Only required if multiple tags are used.
<alpha android:fromAlpha=float
android:toAlpha=float > |
<scale android:fromXScale=float
android:toXScale=float
android:fromYScale=float
android:toYScale=float
android:pivotX=string
android:pivotY=string > |
<translate android:fromX=string
android:toX=string
android:fromY=string
android:toY=string > |
<rotate android:fromDegrees=float
android:toDegrees=float
android:pivotX=string
android:pivotY=string > |
<interpolator tag>
<set>
</set>
XML의 모든 element는 동시에 적용되므로 순차적으로 진행하기 위해서는 startOffset attribute를 사용하여야 한다.
각각의 attribute에 해당하는 자세한 내용은 안드로이드 가이드를 참조하기 바란다.
Menus
이전에도 언급한 내용으로 안드로이드 메뉴는 Options Menu, Context Menu와 Sub Menu이며 이들은 XML에 resource로 정의될 수 있으며, 소스 코드 내에서 MenuInflater를 사용하여 나타난다.
Location: res/menu/some_file.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/example_item
android:title="Example Item"
android:icon="@drawable/example_item_icon" />
<group android:id="@+id/example_group">
<item android:id="@+id/example_item2
android:title="Example Item 2"
android:icon="@drawable/example_item2_icon" />
</group>
<item android:id="@+id/example_submenu
android:title="Example Sub Menu" >
<menu>
<item android:id="@+id/example_submenu_item
android:title="Example Sub Menu Item" />
</menu>
</item>
</menu>
Layout
이전에 설명했던 내용을 참조하기 바란다.
Styles and Themes
이전에 설명했던 내용을 참조하기 바란다.
Searchable
사용자에게 검색을 나타나게 하기 위해 안드로이드 Framework은 Application이 검색방식을 제어할 수 있도록 API를 제공한다.
Location: res/xml/searchable.xml
<searchable xmlns:android="http://schemas.android.com/apk/res/android
android:label="@string/search_label"
... >
<actionkey
android:keycode="KEYCODE_CALL"
... >
</searchable>
각각의 attribute에 해당하는 자세한 내용은 안드로이드 가이드를 참조하기 바란다.