<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                #### Typed Array(數組資源) - 有時候我們會在Java 代碼中定義了兩個數組, Android 并不推薦在Java 代碼中定義數組,因為Android 允許通過資源文件來定義數組資源。 - Typed Array 代表一個通用類型的數組, 該類提供了getXxx(int index)方法來獲取指定索引處的數組元素。 - Android 采用位于/res/values 目錄下的arrays. xrnl 文件來定義數組資源,定義數組時XML資源文件的根元素也是<resources... />,該元素內可包含如下三種子元素。 - <array... ./>子元素: 定義普通類型的數組, 例如Drawable 數組 - <string-array... ./>子元素: 定義字符串數組。 - <integer-array... ./>子元素: 定義整型數組。 - 目前官網已經將integer-array獨立出來了,如下定義資源文件`res/values/integers.xml`, ~~~ <?xml version="1.0" encoding="utf-8"?> <resources> <integer-array name="bits"> <item>4</item> <item>8</item> <item>16</item> <item>32</item> </integer-array> </resources> ~~~ java代碼中可以這樣引用 ~~~ Resources res = getResources(); int[] bits = res.getIntArray(R.array.bits); ~~~ 而在xml文件中這樣引用In XML: `@[package:]array.bits` - 為了能在Java 程序中訪問到實際數組, Resources 提供了如下方法。 - String[] getStringArray(int id): 根據資源文件中字符串數組資源的名稱來獲取實際的字符串數組。 - int[] getlntArray(int id): 根據資源文件中整型數組資源的名稱來獲取實際的整型數組。 - TypedArray obtainTypedArray(int id): 根據資源文件中普通數組資源的名稱來獲取實際的普通數組。 - 示例如下 ~~~ <?xml version="1.0" encoding="utf-8"?> <resources> <!-- 定義一個Drawable數組 --> <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> ~~~ 可以在xml文件中做如下引用,例如,如下界面布局文件中定義了一個ListView 數組, 并將android:entries 屬性值指定為一個數組。界面布局文件代碼如下。 ~~~ <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal"> <!-- 省略其他組件--> <!-- 定義ListView組件,使用了數組資源 --> <ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:entries="@array/books"/> </LinearLayout> ~~~ 在Java代碼就可以做如下操作 ~~~ public class MainActivity extends Activity { // 獲取系統定義的數組資源 String[] texts; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); texts = getResources().getStringArray(R.array.string_arr); // 創建一個BaseAdapter對象 BaseAdapter ba = new BaseAdapter() { @Override public int getCount() { // 指定一共包含9個選項 return texts.length; } @Override public Object getItem(int position) { // 返回指定位置的文本 return texts[position]; } @Override public long getItemId(int position) { return position; } // 重寫該方法,該方法返回的View將作為的GridView的每個格子 @Override public View getView(int position , View convertView, ViewGroup parent) { TextView text = new TextView(MainActivity.this); Resources res = MainActivity.this.getResources(); // 使用尺度資源來設置文本框的高度、寬度 text.setWidth((int) res.getDimension(R.dimen.cell_width)); text.setHeight((int) res.getDimension(R.dimen.cell_height)); // 使用字符串資源設置文本框的內容 text.setText(texts[position]); TypedArray icons = res.obtainTypedArray(R.array.plain_arr); // 使用顏色資源來設置文本框的背景色 text.setBackground(icons.getDrawable(position)); text.setTextSize(20); return text; } }; GridView grid = (GridView) findViewById(R.id.grid01); // 為GridView設置Adapter grid.setAdapter(ba); } } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看