예전에 한참 찾아 돌아댕겼던 자료다.

string.xml에 String선언해 두고 java코드에서 불러다 쓰기!

 

 

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, ResourceRoundup!</string>
    <string name="app_name">ResourceRoundup</string>
    <drawable name="redDrawable">#F00</drawable>
 <color name="prettyTextColor">#00ff00</color>
 <dimen name="textPointSize">14pt</dimen>
 <string-array name="flavors">
 <item>바닐라</item>
 <item>초코</item>
 <item>딸기</item>
 </string-array>
</resources>

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
 <TextView  
 android:id="@+id/textView01"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />
    <ImageView  
    android:id="@+id/imageView02"
    android:layout_width="200dip" 
    android:layout_height="200dip" 
    />
    <TextView  
    android:id="@+id/textView03"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
    <TextView  
    android:id="@+id/textView04"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
    <TextView  
 android:id="@+id/textView05"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />
    <TextView  
 android:id="@+id/textView06"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />
    <TextView  
 android:id="@+id/textView07"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />
</LinearLayout>

 

package com.android.ResourceRoundup;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class ResourceRoundup extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        String myString = getResources().getString(R.string.hello);
        ColorDrawable myDraw = (ColorDrawable)getResources().getDrawable(R.drawable.redDrawable);        
        int myColor = getResources().getColor(R.color.prettyTextColor);
        float myDimen = getResources().getDimension(R.dimen.textPointSize);
        String[] aFlavors = getResources().getStringArray(R.array.flavors);
        
        TextView textview1 = (TextView)findViewById(R.id.textView01);
        textview1.setText(myString);
        
        ImageView imageview2 = (ImageView)findViewById(R.id.imageView02);
        imageview2.setImageDrawable(myDraw);
        
        TextView textview3 = (TextView)findViewById(R.id.textView03);
        textview3.setTextColor(myColor);
        
        TextView textview4 = (TextView)findViewById(R.id.textView04);
        textview4.setTextSize(myDimen);
        
        TextView textview5 = (TextView)findViewById(R.id.textView05);
        textview5.setText(aFlavors[0]);
        
        TextView textview6 = (TextView)findViewById(R.id.textView06);
        textview6.setText(aFlavors[1]);
        
        TextView textview7 = (TextView)findViewById(R.id.textView07);
        textview7.setText(aFlavors[2]);
        
    }
}

 

그다지 중요할것도 없지만...

알아두기 1 : getResources()

getResources().getString

getResources().getColor

getResources().getDemension

getResources().getDrawable

getResources().getStringArray

위에 것들을 이용해 Java코드에서 String.xml의 Resource를 가져다 사용할 수 있다.

 

알아두기 2 : ColorDrawable

이건...머 걍 네모에 색칠하기^^ 정도로 이해하면 될꺼 같네~

저작자 표시 비영리 변경 금지
신고
Posted by DoDo4989

댓글을 달아 주세요