2017-02-16 110 views
0
<TextView 
    android:id="@+id/label" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@+id/label" 
    android:textSize="20px" > 
</TextView> 

這是具有自定義佈局的ListActivity的部分代碼。所以它可以動態地設定價值。爲什麼android:text =「@ + id/label」?有其他用途嗎?Android:爲什麼textView文本屬性值是ID?

回答

0
<TextView 
    android:id="@+id/label" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@+id/label" <-- wrong 
    android:text="@string/label" <-- get text from string.xml 
    android:text="Label"  <-- direct text 
    android:textSize="20px" > 
</TextView> 
0

同意ZeroOne的回答。添加到他你說一下,你正在使用這裏提到它動態變化的TextView 的價值裏面的活動類

TextView tv = (TextView) findViewById(R.id.label); 

在你所使用的ID佈局。您正在使用R.id.label因爲Android有一個

自動產生一個具有對所有對象,字符串引用,資產,佈局,顏色,款式和ID唯一的十六進制代碼R.class

文件(其中大部分最後是靜態的)等,所以你告訴

R.id.label(這是一個int類型的資源ID)

轉到R類別 - >標識 - >變量名

要設置的價值,您的TextView

tv.setText("Hello World"); 

如果如果你使用

android:text="@+id/label" 

它只是放你的佈局文件設置爲textview的文本的值爲@+id/label。這實際上是沒有意義的,除非你想顯示@+id/label ..

如果你的計劃是設置的TextView的值作爲佈局文件本身然後標籤爲ZeroOne說 要麼使用

android:text="label" 

或者

android:text="@string/my_label" // for this to work you need to add reference in strings.xml inside your res folder 
相關問題