2010-09-08 87 views
211

我已經開始學習Android開發,並正在從以下一本書todolist的例子:什麼是「android.R.layout.simple_list_item_1」?

// Create the array list of to do items 
final ArrayList<String> todoItems = new ArrayList<String>(); 

// Create the array adapter to bind the array to the listView 
final ArrayAdapter<String> aa; 
aa = new ArrayAdapter<String>( this, 
           android.R.layout.simple_list_item_1, 
           todoItems 
          ); 
myListView.setAdapter(aa); 

我不明白的正是這種代碼特別是這一行:

android.R.layout.simple_list_item_1 
+0

但爲什麼是一個參數?我只是想用我的數組更新我的列表視圖,其中一個答案顯示了這一點。我不知道爲什麼我需要這個...這是我的問題。謝謝! http://stackoverflow.com/questions/35098789/how-to-update-the-listview-according-to-arraylist/35099000?noredirect=1#comment57929962_35099000 – 2016-01-30 19:05:23

回答

248

扎卡里亞,這是一個引用作爲Android OS的一部分的內置XML佈局文檔,而不是您自己的XML佈局之一。

這裏的佈局,您可以使用的詳細列表: http://developer.android.com/reference/android/R.layout.html
(更新鏈接感謝@Estel:https://github.com/android/platform_frameworks_base/tree/master/core/res/res/layout

實際上,你可以查看該佈局的代碼。

+15

佈局也在你的SDK安裝 – CommonsWare 2010-09-08 00:49:46

+10

嘿,所以他們是。 :P我只是通過在Eclipse中瀏覽android jar而試圖尋找它們,它只是告訴我「Source Not Found」。但是,是的,他們在平臺> android-x> data> res> layout下。好決定。 :) – kcoppock 2010-09-08 01:06:07

+0

感謝您的答案。但是,在ArrayAdapter中調用此佈局的目的是什麼? – Zakaria 2010-09-08 08:52:33

7

android.R.layout.simple_list_item_1,這是res/layout文件夾中的行佈局文件,其中包含listview中您所在行的相應設計。現在我們通過使用mylistview.setadapter(aa)將數組列表項綁定到行佈局;

+2

歡迎使用stackoverflow。感謝您分享您的知識。 – Brett 2013-01-05 07:51:47

5

沒有必要去外部鏈接,你需要的一切位於您的計算機已經上:

的Android \ Android的SDK \平臺\ Android的-X \ DATA \水庫\佈局。

所有android佈局的源代碼位於此處。

7

正如Klap提到的「android.R.layout.simple_list_item_1是一個參考的內置XML文檔的佈局是Android操作系統的一部分」
所有的佈局都位於:SDK \平臺\ android- XX \ DATA \資源\佈局
要查看佈局的XML:
的Eclipse:只需在代碼的某個地方鍵入android.R.layout.simple_list_item_1,按住Ctrl鍵,將鼠標懸停在simple_list_item_1管理,並從出現選擇下拉「在layout/simple_list_item_1.xml中打開聲明「。它會引導您查看XML的內容。
Android Studio:項目窗口 - >外部庫 - > Android X平臺 - > res - >佈局,在這裏您將看到可用佈局的列表。
enter image description here

29

這是android操作系統的一部分。這是定義的XML文件的實際版本。

simple_list_item_1管理:

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1" 
    style="?android:attr/listItemFirstLineStyle" 
    android:paddingTop="2dip" 
    android:paddingBottom="3dip" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" /> 

simple_list_item_2:

<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android" 
    android:paddingTop="2dip" 
    android:paddingBottom="2dip" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 

    <TextView android:id="@android:id/text1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     style="?android:attr/listItemFirstLineStyle"/> 

    <TextView android:id="@android:id/text2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@android:id/text1" 
     style="?android:attr/listItemSecondLineStyle" /> 

</TwoLineListItem> 
4

每阿爾萬:
的Eclipse:只需在鍵入代碼android.R.layout.simple_list_item_1地方,按住Ctrl鍵,將鼠標懸停在simple_list_item_1,並從出現的下拉列表中選擇打開佈局/簡單聲明_list_item_1.xml。它會引導您查看XML的內容。

從那裏,如果你然後通過在編輯器中產生simple_list_item_1.xml標籤懸停,你會看到該文件位於C:\ DATA \應用程序\ Android的\ Android的SDK \平臺\機器人-19 \ data \ res \ layout \ simple_list_item_1.xml(或您的安裝的同等位置)。