温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

10天学通Android开发(6)-资源访问

发布时间:2020-10-25 23:12:18 来源:网络 阅读:302 作者:wanxl 栏目:移动开发

       

  1. 字符串资源

    自动生成目录 Values,生成了三个

    App_name: AndroidManifest.xml使用了

    hello_worldactivity_main.xml

    代码读取:

 Stringstr=getResources().getString(R.string.app_name);

     //   System.out.println(str);

tv=(TextView)findViewById(R.id.tv)

        tv.setText(R.string.app_name);

 

2、颜色资源

android:background="#FF0000",可简化"#F00"

不透明:"#FF00",半透明:"#8F00"

可添加资源:Android xml File类型:value

<resources>

    <Color name="red">#f00</Color>

    <Color name="green">#0f0</Color>

 </resources>

 

调用自己定义:

   android:background="@Color/red"

调用操作系统:

android:background="@Android:Color/black"

 

3、数组资源

创建Value类型的xml文件:

<resources>

    <string-array  name="arr">

        <item >hello</item>

    <item >baba</item>

    </string-array>

</resources>

调用:

       String[] arr=getResources().getStringArray(R.array.arr);

        forString string : arr)

        System.out.println(string);

 

<ListView entries="@array/arr" />

 

4 Drawable资源

AndroidManifest.xml中:android:icon="@drawable/ic_launcher"

不分分辩率,创建drawable目录:

拷进png文件:20150103122610.png

图片按钮,如:

   <ImageView

       android:id="@+id/p_w_picpathView1"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

        android:src="@drawable/20150103122610" />

 

其它按钮背景:

android:background="@drawable/20150103122610"

 

5  菜单资源

menu下默认生成了菜单项:

 

<menu xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"

    tools:context="com.example.resource6.MainActivity">

 

    <item

       android:id="@+id/action_settings"

       android:orderInCategory="100"

       android:title="@string/action_settings"

       app:showAsAction="never"/>

 

</menu>

 

可重写菜单项,菜单的点击事件如下:

 @verride

    publicboolean onOptionsItemSelected(MenuItemitem)

    {

       switch(item.getItemId()){

       case R.id.action_settings:

           new AlertDialog.Builder(this).setTitile("标题").setMessage("对话框").setPositiveButton("关闭",null).show();

         

          break;

       case R.id.action_bar

           Toast.makeText(this, "消息", Toast_LENTH_SHORT).show();

         break;

        

         default:

            break;     

       }

    }


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI