1
2
3
4
5
6
7
String returnUrl;
try{
    returnUrl = new String(request.getSession().getAttribute("returnUrl").toString());
} catch(Exception e){
    LogFile.write("Url exception: " + e.toString());
}
Out.println(returnUrl);
위 코드는 실행되지 않는다. 그 이유는 첫번째 라인에서 String returnUrl 을 초기화 해 주지 않았기 때문이다. String은 class형태이기 때문에 call by reference의 형태를 띠며 try catch에서 설정된 String의 값이 블록을 벗어나면서 메모리 영역이 반환되므로 가르치는 곳이 0이 되버린다.

question mark(?)를 이용하여 xml에서  현재 테마에 맞춰 값을 설정 할 수 있다.

?android:textColorSecondary

?android:attr/textColorSecondary

동일한 의미이며 values/attrs.xml에 설정된 attribute를 이용하는데 이때 해당하는 attribute에 대한 세부 설정은 theme_base.xml에 설정되어있다.

android-support-v7-appcompat 

를 예시로 들면 

•res/layout/abc_action_bar_home.xml 
android:src="?attr/homeAsUpIndicator"

res/values/attrs.xml
<!-- Specifies a drawable to use for the 'home as up' indicator. --> <attr name="homeAsUpIndicator" format="reference"/>

res/values/themes_base.xml
<item name="homeAsUpIndicator">@drawable/abc_ic_ab_back_holo_dark</item>

위와 같은 방식으로 사용하고 있다.

아래는 구글API 가이드의 Accessing Resources 이다.

Referencing style attributes

A style attribute resource allows you to reference the value of an attribute in the currently-applied theme. Referencing a style attribute allows you to customize the look of UI elements by styling them to match standard variations supplied by the current theme, instead of supplying a hard-coded value. Referencing a style attribute essentially says, "use the style that is defined by this attribute, in the current theme."

To reference a style attribute, the name syntax is almost identical to the normal resource format, but instead of the at-symbol (@), use a question-mark (?), and the resource type portion is optional. For instance:

?[<package_name>:][<resource_type>/]<resource_name>

For example, here's how you can reference an attribute to set the text color to match the "primary" text color of the system theme:

<EditText id="text"
   
android:layout_width="fill_parent"
   
android:layout_height="wrap_content"
   
android:textColor="?android:textColorSecondary"
   
android:text="@string/hello_world" />

Here, the android:textColor attribute specifies the name of a style attribute in the current theme. Android now uses the value applied to the android:textColorSecondary style attribute as the value for android:textColor in this widget. Because the system resource tool knows that an attribute resource is expected in this context, you do not need to explicitly state the type (which would be ?android:attr/textColorSecondary)—you can exclude the attr type.


출처: http://developer.android.com/intl/ko/guide/topics/resources/accessing-resources.html

http://stackoverflow.com/a/4771871/5182144

https://github.com/koush/android-support-v7-appcompat

http://stackoverflow.com/q/15008150/5182144

uri로 xmlns:android=“http://schemas.android.com/apk/res/android”를 쓰겠다는 의미로 저 uri의 정보를 가져다 쓰겠다는 의미

To understand why xmlns:android=“http://schemas.android.com/apk/res/android” must be the first in the layout xml file We shall understand the components using an example

Sample::

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/container" >    
</FrameLayout>

Uniform Resource Indicator(URI)

  • In computing, a uniform resource identifier (URI) is a string of characters used to identify a name of a resource.
  • Such identification enables interaction with representations of the resource over a network, typically the World Wide Web, using specific protocols.

Ex:http://schemas.android.com/apk/res/android:id is the URI here


XML Namespace:

  • XML namespaces are used for providing uniquely named elements and attributes in an XML document. xmlns:android describes the android namespace.
  • Its used like this because this is a design choice by google to handle the errors at compile time.
  • Also suppose we write our own textview widget with different features compared to android textview, android namespace helps to distinguish between our custom textview widget and android textview widget

출처 : http://stackoverflow.com/a/25758222

+ Recent posts