溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android----xml文件中的控件的id設(shè)置

發(fā)布時(shí)間:2020-07-14 12:32:54 來源:網(wǎng)絡(luò) 閱讀:4919 作者:wauoen 欄目:移動開發(fā)

    Android開放中要想得到布局文件中控件的引用,該控件必須設(shè)置id屬性,這兩有兩種方式設(shè)置id:(1)@+id/xxxx;(2)@id/xxxx;下面做個(gè)簡單的介紹。

  1. @+id/xxx:如果R文件中沒有該id則創(chuàng)建;

注意:一個(gè)xml文件中不能出現(xiàn)兩個(gè)以該形式設(shè)置同一id的兩個(gè)控件(include標(biāo)簽例外);

示例1 正確的使用:

<TextView
    android:id="@+id/mytv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="hello world"/>

示例2 錯(cuò)誤(兩個(gè)id相同):此時(shí)系統(tǒng)會提醒報(bào)錯(cuò)

<TextView
    android:id="@+id/mytv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="hello world"/>
<TextView
    android:id="@+id/mytv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="hello world"/>

示例3 允許以下用法,但是該id指向的是include標(biāo)簽,之后的linearLayout設(shè)置id的操作無意義:

<include
    android:id="@+id/include1"
    layout="@layout/my"
    android:layout_width="50dp"
    android:layout_height="50dp"/>
<LinearLayout
    android:id="@+id/include1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"/>

如果將include標(biāo)簽與LinearLayout交換位置則會報(bào)錯(cuò)。

示例 4 允許以下用法,但是該id指向TextView,之后的include標(biāo)簽和LinearLayout設(shè)置id無意義:

<TextView
    android:id="@+id/mytv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="hello world"/>
<include
    android:id="@id/mytv"
    layout="@layout/my"
    android:layout_width="50dp"
    android:layout_height="50dp"/>
<LinearLayout
    android:id="@id/mytv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"/>

如果將TextView的位置下移,運(yùn)行會出錯(cuò)。如果include中引用的布局存在與TextView相同的id設(shè)置,不會報(bào)錯(cuò)但是無意義。

2.@id/xxxx:引用ids.xml中相應(yīng)的id,與@+id/xxx不同,一旦向ids.xml文件中添加一個(gè)id在R.java文件中會生成一個(gè)相應(yīng)的id,無論是否有控件使用該id。

使用示例:

(1)創(chuàng)建ids.xml

<resources>
    <item name="hello" type="id" />
    <item name="hello2" type="id" />
    <item name="hello3" type="id" />
    <item name="hello4" type="id" />
    <item name="hello5" type="id" />
    <item name="hello6" type="id" />
    <item name="hello7" type="id" />
    <item name="hello8" type="id" />
</resources>

(2)使用id

<TextView
    android:id="@id/hello"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="hello 1" />

<TextView
    android:id="@id/hello"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="hello 2" />

<TextView
    android:id="@id/hello"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="hello 3" />

多個(gè)控件可以以同樣的方式設(shè)置統(tǒng)一id,但是該id只屬于最先使用該id的控件。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI