zoukankan      html  css  js  c++  java
  • <Android>资源的访问,颜色、字符串、尺寸、XML、DRAWABLES资源分使用

    1.资源的访问

    代码中使用Context的getResources()方法得到Resources对象,访问自己定义的资源R.资源文件类型.资源文件名称,访问系统定义的资源android.R. 资源文件类型.资源文件名称。

    在其他资源中引用资源一般格式为@[包名称:]资源类型/资源名称

      android:textColor=”@color/opaque_red” 

    2.颜色资源的使用

    1.颜色资源xml的定义

     <?xml version="1.0" encoding="utf-8"?>
     <resources>
        <color name="red_bg">#f00</color>
        <color name="blue_text">#0000ff</color>
     </resources>

    2.其他xml资源中引用颜色资源

      android:textColor="@color/blue_text" 

    3.java代码中引用颜色文件

    // 引用颜色资源,设置背景色为红色

     1 getWindow().setBackgroundDrawableResource(R.color.red_bg); 

    补充获得颜色的方法 Resources.getColor();

    3.字符串资源的使用

    1.字符串xml的定义

     <?xml version="1.0" encoding="utf-8"?>
     <resources>
        <string name="app_name">Test Resources</string>
         <string name="test_str1">从代码中引用!</string>
         <string name="test_str2">从资源文件引用!</string>
     </resources>

    2.其他xml文件中引用

      android:text="@string/test_str1" 

    3.java代码中引用

     String str = getString(R.string.test_str2).toString();
     //使用Context.getString()方法,传递资源id参数得到该字符串。

    补充获得字符串资源方法Resources.getString(),java代码中R.string.string_name同颜色资源

    4.尺寸资源的使用

    android中的尺寸有px,in,mm,pt(点,英尺in的1/72),dp,sp

    1.尺寸资源的定义

     <?xml version="1.0" encoding="utf-8"?>
     <resources>
         <dimen name="text_width">150px</dimen>
         <dimen name="text_height">100px</dimen>
         <dimen name="btn_width">30mm</dimen>
         <dimen name="btn_height">10mm</dimen>
     </resources>

    2.其他xml资源中引用尺寸(设置TextView的宽和高)

    android:width="@dimen/text_width"
    android:height="@dimen/text_height"

    3.java代码中引用尺寸(设置Button的宽和高)

    Resources r = getResources();
    float btn_h = r.getDimension(R.dimen.btn_height);
    float btn_w = r.getDimension(R.dimen.btn_width);
    
    myButton.setHeight((int)btn_h);
    myButton.setWidth((int)btn_w);

    补充java代码中可通过R.dimen.dimen_name引用尺寸。

    5.原始xml资源的使用

    1.原始xml文件的定义test.xml

    <?xml version="1.0" encoding="utf-8"?>
    
    <resources>
    
       <customer name="tom" age="20" gender="male" email="tom@yahoo.com"/>
       <customer name="kite" age="21" gender="male" email="kite@yahoo.com"/>
    
    </resources>

    2.java代码中解析原始xml文件

    int counter = 0;
    StringBuilder sb = new StringBuilder("");
    Resources r = getResources();
    XmlResourceParser xrp = r.getXml(R.xml.test);
    try { while (xrp.getEventType() != XmlResourceParser.END_DOCUMENT) { if (xrp.getEventType() == XmlResourceParser.START_TAG) { String name = xrp.getName(); if(name.equals("customer")){ counter++; sb.append("第"+counter+"条客户信息:"+" "); sb.append(xrp.getAttributeValue(0)+" "); sb.append(xrp.getAttributeValue(1)+" "); sb.append(xrp.getAttributeValue(2)+" "); sb.append(xrp.getAttributeValue(3)+" "); } } else if (xrp.getEventType() == XmlPullParser.END_TAG) { } else if (xrp.getEventType() == XmlPullParser.TEXT) { } xrp.next(); } myTextView.setText(sb.toString()); } catch (XmlPullParserException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }

    补充好像只能在java代码中引用原始xml文件资源

    6.drawables资源的使用

    drawables资源分为Bitmap(位图文件),Color Drawable(颜色),Nine-Patch Image(九片图片)三类

    xml中使用位图文件时,引用g1.jpg

    android:src="@drawable/g1"

    java代码中引用位图文件时

    Resources r = getResources();
    Drawable d = r.getDrawable(R.drawable.moto);
    myImageView.setImageDrawable(d);
  • 相关阅读:
    关于Java中System.currentTimeMillis和System.nanoTime的错误认识
    多线程以外
    vim 小技巧
    Virtual Box HostOnly网络模式配置
    How 30 Minutes a Day Can Increase Your Intelligence
    YUM命令使用
    Hash算法及其应用
    jetty + apache httpd 反向代理配置
    使用SCTP优化网络
    .NET书籍推荐
  • 原文地址:https://www.cnblogs.com/lshs/p/4397173.html
Copyright © 2011-2022 走看看