2012-12-01 22 views
-1

剛剛我對android非常陌生,我有兩個android:id。結合兩個簡單的android:id或(R.id)

例如:[R.id.custom_font]和[R.id.product_name]

在java文件

TextView tv = (TextView)findViewById(R.id.custom_font); 
Typeface cFont = Typeface.createFromAsset(getAssets(), "fonts/jcc.ttf"); 
tv.setTypeface(cFont); 


adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, R.id.custom_font, products); 
lv.setAdapter(adapter); 

當我在單個文本把它們放在一起查看它示出了一個錯誤消息:

[屬性 「機器人ID」 已經被用於元件 「的TextView」 指定在.xml文件

<TextView 

    android:textColor="?android:textColorPrimary" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/custom_font" 
    android:id="@+id/product_name" //Attribute "android:id" was already specified for element "TextView" 
    android:textSize="15sp" /> 

如何在單個文本視圖中傳遞兩個android:id?

或其他人,如果好心幫我Download This

在此先感謝。

+1

爲什麼不用'TextView tv =(TextView)findViewById(R.id.custom_font);'和'adapter = new ArrayAdapter ''使用相同的id? –

+0

**「我怎樣才能在單個文本視圖中傳遞兩個android:id?」** - 你不能......就這麼簡單。艾哈邁德所說的是真實的 - 對於任何UI元素(小部件)你只能有一個資源ID。這正是你遇到錯誤的原因。 – Squonk

+0

@Squonk我發現在android中使用自定義字體[這裏](http://www.barebonescoder.com/2010/05/android-development-using-custom-fonts/)。假設這裏有一個'android:id' ** android:id =「@ + id/CustomFontText」**但是在我的項目中我已經有了一個** android:id =「@ + id/product_name」產品名稱列表中。 **那麼我怎麼能一起實現呢?**如果我使用'兩個id'或'相同的id'。模擬器顯示錯誤消息**「應用程序意外停止,請稍後再試!」**我真的很無奈! –

回答

1

您只能將一個ID設置爲UI小部件。刪除其中一個Id。你可以爲你的ArrayAdapter使用相同的Id。

0

幾乎相同的答案艾哈邁德,但基於的評論,你似乎並沒有得到它,那麼改變你這樣的XML(刪除給出了錯誤的行):

<TextView 
android:textColor="?android:textColorPrimary" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:id="@+id/custom_font" 
android:textSize="15sp" /> 

然後改變Java代碼:

TextView tv = (TextView)findViewById(R.id.custom_font); 
Typeface cFont = Typeface.createFromAsset(getAssets(), "fonts/jcc.ttf"); 
tv.setTypeface(cFont); 


adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.custom_font, R.id.custom_font, products); 
lv.setAdapter(adapter); 

如果說你的問題是與構造對於那些困擾你同一個ArrayAdapter的兩個參數,嘗試像這樣...

更改XML這樣的:

<FrameLayout 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:id="@+id/product_name"> 

<TextView 
android:textColor="?android:textColorPrimary" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:id="@+id/custom_font" 
android:textSize="15sp" /> 

</FrameLayout> 

然後你就可以保持相同的Java代碼,你有什麼...

如果仍然沒有解決它,我爲你的問題實在不明白真的是。