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

       

网站建设哪家好,找创新互联!专注于网页设计、网站建设、微信开发、微信小程序定制开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了特克斯免费建站欢迎大家使用!

  1. 字符串资源

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

    App_name: AndroidManifest.xml使用了

    hello_world:activity_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);

       for(String 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;     

       }

    }

名称栏目:10天学通Android开发(6)-资源访问
网站路径:https://www.cdcxhl.com/article2/pgggic.html

成都网站建设公司_创新互联,为您提供定制网站商城网站外贸建站网站内链电子商务

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

网站托管运营