2011-11-04 26 views
1

我想在我的ExpandableList的一行中添加一個小圖像。您可以在下面看到我的自定義適配器的getchildview方法的代碼。它適用於設置動態文本,但當我想添加圖像時失敗。無法設置圖像,同時充氣在android上的佈局

我不明白爲什麼用於添加圖像的命令完全被應用程序忽略。我試過2個版本,只是爲了看到兩個失敗。

我沒有得到任何運行時錯誤,只是圖像沒有加載。 此外,我知道至少「setImageBitmap」的作品,因爲我在其他地方成功地使用它。

引用的圖像是9x10 png文件。

請幫助我,因爲我不認爲我錯過了任何東西,但圖像根本不顯示。

我試圖在relativelayout l上設置背景顏色(setBackgroundColor),但它也沒有效果。所以我認爲它與圖形有關,但我無法在我的方法中找到任何錯誤。

public View getChildView(int groupPosition, int childPosition, 
      boolean isLastChild, View convertView, ViewGroup parent) { 
     RelativeLayout l = (RelativeLayout) getLayoutInflater().inflate(R.layout.titoli2_gruppo_riga, null) 
     String s; 
     s=prodotti.get(groupPosition).get(childPosition)[DSP]; 
     Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.disponibile); 
     if(s.equals("true"))      
      ((ImageView) l.findViewById(R.id.tgr_dispon)).setImageBitmap(bm); 
     else 
      ((ImageView) l.findViewById(R.id.tgr_dispon)).setImageResource(R.drawable.nondispon);   
     ((TextView) l.findViewById(R.id.tgr_descr)).setText(prodotti.get(groupPosition).get(childPosition)[DES]); 
     ((TextView) l.findViewById(R.id.tgr_prezz)).setText("€"+prodotti.get(groupPosition).get(childPosition)[PRE]); 
     ((TextView) l.findViewById(R.id.tgr_note)).setText(prodotti.get(groupPosition).get(childPosition)[NOT]);     
     ((TextView) l.findViewById(R.id.tgr_sigla)).setText(prodotti.get(groupPosition).get(childPosition)[SIG]); 


     return l; 
} 

你可以看到XML我正在膨脹,這裏http://pastebin.com/DwxdxJSK

回答

1

你有沒有核對之後圖形佈局在你的XML文件,圖片是不是在你看到圖形佈局因爲TextView with id as tgr_descr is given match_parent所以你Imageview不可見。

檢查圖像是否是可見的只是使用android:src="@drawable/icon"

試試這個代碼:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="horizontal" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" android:weightSum="1"> 
<ImageView  android:id="@+id/tgr_dispon" android:layout_height="wrap_content" 
       android:layout_width="wrap_content" android:layout_toLeftOf="@+id/tgr_descr" 
       android:layout_alignParentLeft="true" android:paddingLeft="7dp" 
       android:src="@drawable/icon"/> 
<TextView  android:id="@+id/tgr_descr" android:layout_height="wrap_content" 
       android:layout_width="wrap_content" android:layout_alignParentTop="true" 
       android:layout_toLeftOf="@+id/tgr_prezz" android:text="descr" 
       android:textAppearance="?android:attr/textAppearanceMedium" android:padding="7dp"/> 

<TextView  android:id="@+id/tgr_prezz" android:layout_height="wrap_content" 
       android:layout_width="wrap_content" android:layout_alignParentTop="true" 
       android:layout_alignParentRight="true" android:text="prezzo" 
       android:textAppearance="?android:attr/textAppearanceMedium" android:padding="7dp"/> 

<TextView  android:id="@+id/tgr_note" android:layout_height="wrap_content" 
       android:layout_width="wrap_content" android:layout_below="@+id/tgr_descr" 
       android:layout_alignParentLeft="true" android:text="note" 
       android:textAppearance="?android:attr/textAppearanceSmall" android:paddingLeft="7dp"/> 
<TextView  android:id="@+id/tgr_sigla" android:layout_height="wrap_content" 
       android:layout_width="wrap_content" android:layout_toRightOf="@+id/tgr_note" 
       android:layout_below="@+id/tgr_descr" android:layout_alignParentRight="true" 
       android:text="sigla" android:textAppearance="?android:attr/textAppearanceSmall" 
       android:paddingLeft="7dp"/> 
</RelativeLayout> 
+0

我會測試你的建議。我很快就會回來 –

+0

不,不好意思。圖像也沒有顯示。 –

+0

無論如何,你的方向一直很有用,我現在重新測試我的XML塊,以塊查找隱藏組件:) –