android中获取资源

在res/values/string.xml中加入以下代码

//常规

<string name="app_name">Demo1</string>

<string name="hello_world">Hello qq!</string>

<string name="action_settings">Settings</string>

<string name="app_desc">11</string>

//颜色

    <color name="prettyTextColor">#ff0000</color>

//尺寸

    <dimen name="textPointSize">14pt</dimen>

//图形

    <drawable name="redDrawable">#F00</drawable>

//字符串数组

    <string-array name="flavors">
        <item >Vanilla</item>
        <item >Chocolate</item>
        <item >Strawberry</item>

    </string-array>

//带逗号的不处理

    <string name="test_string">Testing 1,2,3</string>

//单引号和双引号需要转义,如果不转义需要用双引号引起来

    <string name="test_tring2">Testing \'4\',\"5\","‘6"</string>

//粗体、斜体、带下划线的字符串

    <string name="txt"><b>Bold</b>,<i>Italic</i>,<u>Line</u></string>

//使用格式化的字符串资源

<string name="winLose">Score:%1$d of %2$d! You %3$s.</string>

<string name="winLoseStyled">Score:%1$d of %2$d! You &lt;i&gt;%3$&lt;/i&gt;.</string>

//使用带数字的字符串

<plurals name="quantityOfGeese">
   <item quantity="one">You caught a goose!</item>
   <item quantity="other"> You caught %d geese!</item>
</plurals>

注:zero用于表示零数量的单词

one用于表示单个数量的单词

two表示两个数量的单词

few用于表示小数量的单词

many用于表示大数量的单词

other用于表示没有数量形式的单词

//使用bool类型

<bool name="onePlusOneEqualsTwo">true</bool>
    <bool name="isAdvancedFeaturesEnabled">false</bool>


//使用整型资源

 <integer name="numTimesToRepeat">25</integer>
    <integer name="startingAgeOfCharacter">3</integer>

//使用颜色资源

 <color name="background_color">#006400</color>
    <color name="test_color">#FFE4C4</color>


//使用尺寸资源

<dimen name="FourteenPt">14pt</dimen>
    <dimen name="OneInch">lin</dimen>
    <dimen name="TenMillimeters">10mm</dimen>
    <dimen name="TenPixels">10px</dimen>





在java代码中加入代码

//常规取值
String myString = getResources().getString(R.string.hello_world);
int myColor = getResources().getColor(R.color.prettyTextColor);
float myDimen = getResources().getDimension(R.dimen.textPointSize);
//ColorDrawable myDraw = (ColorDrawable)getResources().getDrawable(R.drawable.redDrawable);
String[] flavors = getResources().getStringArray(R.array.flavors);
Log.i(DEBUG_TAG, "myString="+ myString + ",myColor=" + myColor + ",myDimen= "+myDimen+",flavors="+flavors.toString());

//单引号双引号取值
String test_string = getResources().getString(R.string.test_string);
String test_string2 = getResources().getString(R.string.test_tring2);
String txt = getResources().getString(R.string.txt);

Log.i(DEBUG_TAG, "test_string=" + test_string+",test_string2="+test_string2+",txt="+txt);
//处理带HTML格式的字符串资源
String myStyledWinString;
myStyledWinString = getResources().getString(R.string.winLoseStyled);
Log.d(DEBUG_TAG, "myStyledWinString=" + myStyledWinString);
String result = String.format(myStyledWinString, 5,5, escapedWin);
Log.d(DEBUG_TAG, "result=" + result);
CharSequence styledResults= Html.fromHtml(result);
Log.d(DEBUG_TAG, "styledResults=" + styledResults);


//使用带数字的字符串
int quantity = getQuantityOfGeese();
Resources plurals = getResources();
String geeseFound = plurals.getQuantityString(R.plurals.quantityOfGeese, quantity, quantity);
Log.d(DEBUG_TAG, "geeseFound=" + geeseFound);


//使用bool类型
boolean isAdvancedMode = getResources().getBoolean(R.bool.isAdvancedFeaturesEnabled);
Log.d(DEBUG_TAG, "isAdvancedMode=" + isAdvancedMode);

//使用整型资源
int repTimes = getResources().getInteger(R.integer.numTimesToRepeat);
Log.d(DEBUG_TAG, "repTimes=" + repTimes);
//使用颜色资源
int myResourceColor = getResources().getColor(R.color.background_color);
Log.d(DEBUG_TAG, "myResourceColor=" + myResourceColor);

//使用尺寸资源
float myDimension = getResources().getDimension(R.dimen.textPointSize);
Log.d(DEBUG_TAG, "myResourceColor= "+myResourceColor);


//获取图片资源并替换为新的图片资源
ImageView flagImageView = (ImageView)findViewById(R.id.imageView1);
flagImageView.setImageResource(R.drawable.image2);

//获取图片资源并取得其高度和宽度
BitmapDrawable bitmapFlag = (BitmapDrawable)getResources().getDrawable(R.drawable.image1);
int ibitmapHeightInPixels = bitmapFlag.getIntrinsicHeight();
int ibitmapWidthInpixels = bitmapFlag.getIntrinsicWidth();

Log.d(DEBUG_TAG, "ibitmapHeightInPixels=" + ibitmapHeightInPixels + ",ibitmapWidthInpixels="+ibitmapWidthInpixels);

//如果使用的是9patch图形可以返回一个专门的NinePatchDrawable对象
NinePatchDrawable stretchy = (NinePatchDrawable)getResources().getDrawable(R.drawable.image2);
int stretchyHeightInPixels = stretchy.getIntrinsicHeight();
int stretchyWidthInpixels = stretchy.getIntrinsicWidth();
Log.d(DEBUG_TAG, "stretchyHeightInPixels=" + stretchyHeightInPixels + ",stretchyWidthInpixels="+stretchyWidthInpixels);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值