Android 資源的使用

來源:互聯網
上載者:User
Android 資源的使用

一、dimension資源的使用

Android中dimension單位有:  px 像素 dp 密度  mm 毫米  pt 點   sp 刻度  in英寸

dimension資源的定義

<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="px">10px</dimen>    <dimen name="dp">10dp</dimen>    <dimen name="sp">70sp</dimen></resources>

dimension資源的使用有兩種方法

一種在Layout中用:

    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world"         android:textSize="@dimen/sp"/>

 一種在java檔案中用:

this.getResources().getDimension(R.dimen.dp);

 

二、資源String,StringArrary,plurals的使用

xml檔案如下:

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="app_name">Resource_Plurals</string>    <string name="action_settings">Settings</string>    <string name="hello_world">Hello world!</string>    <string name="format_msg">we are %1$s, I am %2$d years old.</string>    <plurals name="student">          <item quantity="one">I am 1 student</item>          <item quantity="other">We are %d students</item>      </plurals>    <string-array name="list">        <item>Item1</item>        <item>Item2</item>        <item>Item3</item>        <item>Item4</item>    </string-array></resources>

 資源的使用:

@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Resources resources = getResources();String text = String.format( getString(R.string.format_msg), "eworker",21);Log.i("app_name", text);          String[] listsStrings =resources.getStringArray(R.array.list);String string = resources.getQuantityString(R.plurals.student, 10, 10);Log.i("plurals", string);String string3 = resources.getQuantityString(R.plurals.student, 50, 50);Log.i("plurals", string3);}

  1、 getString(R.string.format_msg) 這個方法可以獲得想要的資源。

2、String text = String.format( getString(R.string.format_msg), "eworker",21);

<string name="format_msg">we are %1$s, I am %2$d years old.</string> 這條資源1$s代表字元, %2$d代表數字,所有要進行格式轉化。

3、String[] listsStrings = resources.getStringArray(R.array.list);這是一個數組列表。

4、String string = resources.getQuantityString(R.plurals.student, 10, 10); 這個使用的是plurals。

最終的顯示的內容為We are 10 students; We are 50 students

最終的結果可以看:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.