007.資源運用:res

  • 布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.app7.MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/hello_world"
        android:textSize="32sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onClick1"
            android:text="Button" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onClick2"
            android:text="Button" />

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onClick3"
            android:text="Button" />

        <Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onClick4"
            android:text="Button" />
    </LinearLayout>

</LinearLayout>
  • 文件檔 res\raw\aaa.txt存檔格式須為Utf-8、Big-5
  • 記事本預設存檔格式為ANSI,必須更改

  • values\color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="c1">#ff0000</color>
    <color name="c2">#0000ff</color>

</resources>
  • valuse\string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">App7</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="str1">Hello Android</string>

</resources>
  • MainActivity.java
package com.example.app7;

import java.io.InputStream;
import java.util.Scanner;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {

    TextView textView;
    Resources res;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = (TextView) this.findViewById(R.id.textView1);
        res = this.getResources();

    }

    public void onClick1(View view) {

        //textView.setText("Hello Java");//字串型態
        // textView.setText(R.string.str1);
        String text = res.getString(R.string.str1);
        textView.setText(text);
    }

    public void onClick2(View view) {
        textView.setTextColor(Color.RED);
        int c = res.getColor(R.color.c1);
        textView.setTextColor(c);
        // textView.setTextColor(res.getColor(R.color.c1));
    }

    @SuppressWarnings("deprecation")
    public void onClick3(View view) {
//        textView.setBackgroundResource(R.color.c2);
        Drawable dr = res.getDrawable(R.color.c2);
//        Drawable dr = res.getDrawable(R.drawable.ic_launcher);//圖片
//        textView.setBackgroundDrawable(dr);        
        textView.setBackground(dr);
    }


    public void onClick4(View view){
        InputStream is=  res.openRawResource(R.raw.aaa);
        Scanner sc = new Scanner(is);
        textView.setText("");
        while(sc.hasNextLine()){
            textView.append(sc.nextLine());
            textView.append("\n");
        }    
    }
}