zoukankan      html  css  js  c++  java
  • Android开发 ---如何操作资源目录中的资源文件5 ---Raw资源管理与国际化

    效果图:

     

    1、activity_main.xml

      描述:

        定义两个按钮,一个是Raw资源管理,一个是处理国际化语言,其中i18n表示简体中文

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Raw资源管理"
            android:onClick="test_5"
            />
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/i18n"
            android:onClick="test_5"
            />
    </LinearLayout>

    2、MainActivity.java

      描述:

        页面跳转

    package com.example.android_shaperesoucesdemo;
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    public class MainActivity extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
        }
        public void test_5(View view){
            Intent intent = new Intent(this,RawActivity.class);
            startActivity(intent);
        }
    }

    3、activity_raw.xml

       描述:

         定义两个按钮,一个实现读取Raw资源文件中的文本资源;一个实现读取Raw资源文件中的音频资源

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/activity_raw"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/showMessage"
            />
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="读取Raw中文本资源"
            android:onClick="readTxt"
            />
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="读取Raw中音频资源"
            android:onClick="readMp3"
            />
    </LinearLayout>

    4、RawActivity.java

    package com.example.android_shaperesoucesdemo;
    import android.app.Activity;
    import android.media.MediaPlayer;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.TextView;
    
    import java.io.IOException;
    import java.io.InputStream;
    
    public class RawActivity extends Activity {
        private TextView showMessage;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_raw);
            showMessage = (TextView)findViewById(R.id.showMessage);
        }
        //读取文本资源
        public void readTxt(View view){
         //定义一个输入流,读取raw文件中的hello文件
         //Android读取asserts和raw文件夹下的文件 经常需要用到读取“/res/raw”和"/asserts"文件夹下的文件 InputStream input
    = getResources().openRawResource(R.raw.hello); try {
           //获取流的大小
    int size = input.available();
           //将流转换为字节
    byte[] bytes = new byte[size];
           //读出字节 input.read(bytes); input.close();
           //将字节转换成字符串显示在UI界面上 showMessage.setText(
    new String(bytes)); } catch (IOException e) { e.printStackTrace(); } }
      //定义一个读取MP3文件资源的方法
    public void readMp3(View view) throws IOException{
         //MediaPlayer是播放音频和视频的组件
         //通过组件获取raw中的音乐文件nobody MediaPlayer mp
    = MediaPlayer.create(this,R.raw.nobody);
         //开始播放音乐 mp.start(); } }

    5、res目录下创建一个raw包,在包中创建一个文本文件hello.txt,并在包中放一首音乐nobody.mp3

    在hello.txt文件中随便输入一些内容

    6、处理国际化:

      在res资源目录下创建一个values-zh-rCN的包,包中创建一个String.xml文件 

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="app_name">Android_Resouces_2</string>
    
        <!--定义字符串-->
        <string name="i18n">国际化</string>
    </resources>

      在main目录下创建一个assets的资源目录

     

        然后在assets目录下放入一个SIMKAL.TTF文件,这个文件是简体中文

        文件下载地址:http://www.font5.com.cn/zitixiazai/1/534.html

    如果您发现博客内容有什么错误,请您下方留言
  • 相关阅读:
    Docker化高可用redis集群
    机器学习理论研究方法探讨
    (转载)iOS系统Crash文件分析方法
    ios 学习总结之动画(转)
    (转)iOS sqlite :truncate/delete/drop区分
    (转载)自定义 setDateFormat 显示格式
    (转载)IOS中UIScrollView的属性和委托方法
    vue 实现分页加载数据
    深入理解JQuery插件开发
    博客迁移到GitCafe
  • 原文地址:https://www.cnblogs.com/zn615/p/8192993.html
Copyright © 2011-2022 走看看