@color/c1@color/c2@color/c3@color/c4@colo" />

亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

初學(xué)Android,字符串,數(shù)字,尺寸,數(shù)組資源(十二)

系統(tǒng) 2038 0

下面是使用字符串,數(shù)字,尺寸,數(shù)組資源的例子,挺好,一個(gè)例子已經(jīng)囊括了不少Android資源的使用方法

arrays.xml

    <?xml version="1.0" encoding="utf-8"?>
<resources>
    <array name = "plain_arr">
        <item>@color/c1</item>
        <item>@color/c2</item>
        <item>@color/c3</item>
        <item>@color/c4</item>
        <item>@color/c5</item>
        <item>@color/c6</item>
        <item>@color/c7</item>
        <item>@color/c8</item>
        <item>@color/c9</item>
    </array>
    <string-array name = "string_arr">
        <item >@string/c1</item>
        <item >@string/c2</item>
        <item >@string/c3</item>
        <item >@string/c4</item>
        <item >@string/c5</item>
        <item >@string/c6</item>
        <item >@string/c7</item>
        <item >@string/c8</item>
        <item >@string/c9</item>
    </string-array>
    <string-array name = "books">
        <item >Java</item>
        <item >Ajax</item>
        <item >Android</item>
    </string-array>
</resources>
  
colors.xml

    <?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="c1">#F00</color>
    <color name="c2">#0F0</color>
    <color name="c3">#00F</color>
    <color name="c4">#0FF</color>
    <color name="c5">#F0F</color>
    <color name="c6">#FF0</color>
    <color name="c7">#07F</color>
    <color name="c8">#70F</color>
    <color name="c9">#F70</color>
</resources>
  
dimens.xml
    <?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="spacing">8dp</dimen>
    <dimen name="cell_width">60dp</dimen>
    <dimen name="cell_height">66dp</dimen>
    <dimen name="title_font_size">18sp</dimen>
</resources>
  
strings.xml

    <?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, ValuesResTest!</string>
    <string name="app_name">字符串,數(shù)字,尺寸,數(shù)組資源</string>
    <string name="c1">F00</string>
    <string name="c2">0F0</string>
    <string name="c3">00F</string>
    <string name="c4">0FF</string>
    <string name="c5">F0F</string>
    <string name="c6">FF0</string>
    <string name="c7">07F</string>
    <string name="c8">70F</string>
    <string name="c9">F70</string>
</resources>
  

主界面xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        android:gravity="center"
        android:textSize="@dimen/title_font_size"
         />

    <GridView
        android:id="@+id/grid01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:horizontalSpacing="@dimen/spacing"
        android:verticalSpacing="@dimen/spacing"
        android:numColumns="3" 
        android:gravity="center">
    </GridView>

    <ListView
        android:id="@+id/listView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:entries="@array/books"
        >
    </ListView>

</LinearLayout>
  

代碼

    package WangLi.Resources.Values;

import android.app.Activity;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

public class ValuesResTest extends Activity {
    /** Called when the activity is first created. */
	String[] texts;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        texts = getResources().getStringArray(R.array.string_arr);
        //創(chuàng)建一個(gè)BaseAdapter對(duì)象
        BaseAdapter ba = new BaseAdapter()
        {
        	public int getCount()
        	{
        		return texts.length;
        	}
        	
        	public Object getItem(int position)
        	{
        		return texts[position];
        	}
        	
        	public long getItemId(int position)
        	{
        		return position;
        	}
        	
        	public View getView(int position,View convertView,ViewGroup parent)
        	{
        		TextView text = new TextView(ValuesResTest.this);
        		Resources res = ValuesResTest.this.getResources();
        		//使用尺寸資源來設(shè)置文本框的高度,寬度
        		text.setWidth((int)res.getDimension(R.dimen.cell_width));
        		text.setHeight((int)res.getDimension(R.dimen.cell_height));
        		//使用字符串資源設(shè)置文本框的內(nèi)容
        		text.setText(texts[position]);
        		TypedArray icons = res.obtainTypedArray(R.array.plain_arr);
        		//使用顏色資源來設(shè)置文本框的背景色
        		text.setBackgroundDrawable(icons.getDrawable(position));
        		text.setTextSize(20);
        		return text;
        	}
        };
        GridView grid = (GridView)findViewById(R.id.grid01);
        grid.setAdapter(ba);
    }
}
  

從上面可以看到,定義了字符串,數(shù)字,尺寸,數(shù)組資源xml文件,在代碼中能很容易的使用到它們,在動(dòng)態(tài)生成的R資源類里面可以很輕松的找到它們,系統(tǒng)運(yùn)行效果如下

初學(xué)Android,字符串,數(shù)字,尺寸,數(shù)組資源(十二)


初學(xué)Android,字符串,數(shù)字,尺寸,數(shù)組資源(十二)


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 国内精品自在欧美一区 | 亚洲视频在线网站 | 亚洲精品乱码久久久久久麻豆 | 99久久免费费视频在线观看 | av天天看 | 亚洲精品第四页中文字幕 | 久久国产视频在线观看 | 久久中文字幕在线观看 | 日本中文字幕视频在线看 | 久久er热这里只有精品23 | 日本大片免费一级 | 天天干天天插 | 欧美精品亚洲人成在线观看 | 日韩有码在线播放 | 欧美成人午夜视频 | 四虎影视永久免费观看网址 | 久久免费播放 | 精品久久久久久国产91 | 国产一区私人高清影院 | 久久精品一区 | 国产精品一区在线观看你懂的 | 中文字幕日本在线 | 日韩精品一区二区三区视频 | 亚洲毛片在线观看 | 成人永久免费高清 | 成人区在线观看免费视频 | 91精品国产91久久久久青草 | 久久资源365| 亚洲国产精久久久久久久春色 | 久久欧美精品欧美九久欧美 | 欧美中文在线 | 国产精品亚洲专一区二区三区 | 欧美曰韩免费视频一区 | 不卡的 | 亚洲视频在线观看免费 | 奇米111| 一级骚片超级骚在线观看 | 亚洲 欧美 日韩 在线 | 香蕉久久综合精品首页 | 久亚洲精品不子伦一区 | 国产在线拍揄自揄视频不卡99 |