android中的使用者資源訪問(一)

來源:互聯網
上載者:User

標籤:des   android   http   io   ar   color   使用   sp   java   

這幾天要總結一下android開發中的使用者資源訪問。

android中的使用者資源存在項目工程中res檔案夾下,有字串、顏色、大小、數組、布局、樣式、主題等資源,這些資源可以在xml檔案中引用,也可以在android源碼檔案中使用,今天總結一下字串、顏色、大小、數組、布局和圖片資源。

總的來說,在xml檔案中引用的格式為[<package>.]@/XXX/name;在源碼中引用格式是[<package>.]R.XXX.name。

先貼上在xml檔案中引用的代碼(在布局xml檔案中)和在源碼中引用代碼。

//main.xml布局檔案<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/LinearLayout1"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="main.testresources.MainActivity" >    <TextView        android:id="@+id/tv1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textColor="@color/lor"        android:textSize="@dimen/dim"        android:text="@string/tv1"        android:background="@drawable/ic_launcher" />    <Button         android:id="@+id/btn1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/btn1"        android:textColor="@color/lor2"        android:textSize="@dimen/dim2"        android:background="@drawable/ic_launcher"/>    <!-- 在ADT 16.0以上,在一些沒有文本顯示的控制項裡,需要加上功能描述,即 android:contentDescription="@string/jieshao";android:src="@drawable/test"是設定圖片顯示 -->    <ImageView         android:id="@+id/iv1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:contentDescription="@string/jieshao"        android:src="@drawable/test"/>    <!-- 引用外部布局檔案,這樣會將外部布局檔案添加到該布局檔案中,成為該布局的一部分,布局檔案名稱layout_test.xml-->    <include layout="@layout/layout_test" />    </LinearLayout>
//layout_test.xml布局檔案,供main.xml檔案調用<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/LinearLayout1"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal" >        <Button            android:id="@+id/button1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/Button1" />        <Button            android:id="@+id/button2"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/Button2" /></LinearLayout>
//android源碼檔案public class MainActivity extends ActionBarActivity implements OnClickListener{    protected TextView tv1=null;    protected Button btn1=null;    protected int num=0;    protected StringBuffer stringBuffer=null;    protected String[] arr=new String[3];    protected String str=null;    protected int col=0;    protected float dim=0;    protected ImageView iv=null;        @SuppressLint("NewApi") @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);//引用布局檔案,並載入顯示                tv1=(TextView)findViewById(R.id.tv1);        btn1=(Button)findViewById(R.id.btn1);        iv=(ImageView)findViewById(R.id.iv1);                str=getResources().getString(R.string.tv1);//取得字串資源        tv1.setText(str);                col=getResources().getColor(R.color.lor);//取得顏色資源        btn1.setTextColor(col);//設定文字顯示顏色        btn1.setBackgroundColor(col);//設定按鈕背景顏色                dim=getResources().getDimension(R.dimen.dim);//取得大小資源        btn1.setTextSize(dim);//設定顯示文字大小                arr=getResources().getStringArray(R.array.world);//取得數組資源        btn1.setText(arr[0]);//取數組中的第一個元素作為按鈕顯示文本                tv1.setBackground(getResources().getDrawable(R.drawable.ic_launcher));//取得圖片資源,並設定成背景圖片        iv.setImageResource(R.drawable.test);//設定顯示圖片        btn1.setOnClickListener(MainActivity.this);    }

(1)、字串資源

存在位置:/res/values/strings.xml,根項目是<resources></resources>,用<string></string>標記,如下代碼:

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="app_name">TestResources</string>    <string name="hello_world">Hello world!</string>    <string name="action_settings">Settings</string>    <string name="tv1">第一個</string>    <string name="btn1">點我切換顯示</string></resources>

(2)、顏色資源

存在位置:/res/values/colors.xml,根項目為<resources></resources>,用<color></color>標記,如下代碼

<?xml version="1.0" encoding="utf-8"?><resources>    <!-- 半透明,紅色 -->    <color name="lor">#66ff0000</color>    <!-- 半透明,綠色 -->    <color name="lor2">#6600ff00</color></resources>

(3)、大小資源

存在位置:/res/values/dimens.xml,根項目為<resources></resources>,用<dimen></dimen>標記,如下代碼

<resources>    <!-- Default screen margins, per the Android Design guidelines. -->    <dimen name="activity_horizontal_margin">16dp</dimen>    <dimen name="activity_vertical_margin">16dp</dimen>    <dimen name="dim">30dp</dimen>    <dimen name="dim2">40dp</dimen></resources>

(4)、數組資源

存在位置:/res/values/arrays.xml,根項目為<resources></resources>,用<integer-array></integer-array>或<string-array></string-array>標記,如下代碼

<?xml version="1.0" encoding="utf-8"?><resources>    <integer-array name="hello">        <item >1</item>        <item >2</item>        <item >3</item>    </integer-array>    <string-array name="world">        <item >one</item>        <item >two</item>        <item >three</item>    </string-array></resources>

(5)、布局檔案資源

存在位置:/res/layout/xxx.xml,根項目不定,一般是<LinearLayout></LinearLayout>,用各個元件節點標記,見上述貼出的布局xml檔案

(6)、圖片資源

存在位置:/res/drawable-xxxx/圖片名,非xml檔案






android中的使用者資源訪問(一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.