【问题标题】:cannot set image while inflating layout on android在android上膨胀布局时无法设置图像
【发布时间】:2011-12-22 01:23:19
【问题描述】:

我正在尝试在我的 ExpandableList 的一行中添加一个小图像。下面你可以看到我的自定义适配器的getchildview方法的代码。它非常适合设置动态文本,但是当我想添加图像时它会失败。

我不明白为什么应用程序会完全忽略添加图像的命令。我尝试了 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

【问题讨论】:

    标签: android xml image layout bitmap


    【解决方案1】:

    您是否在 XML 文件中检查了 图形布局图像在图形布局中不可见,因为 TextView with id as tgr_descr is given match_parent 所以您的 Imageview 不可见。 p>

    要检查图像是否可见,只需使用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>
    

    【讨论】:

    • 我会测试你的建议。我很快就会回来
    • 无论如何你的指导很有用,我现在正在逐块重新测试我的 XML 以找到隐藏组件:)
    • @STTLCU 我检查了..它正在工作.. 分配 String s="true" 并尝试静态值
    • 当然我完全删除了字符串检查部分来尝试代码:)
    • @STTLCU 在您的图形布局中是否可以查看您的图像?
    猜你喜欢
    • 2015-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-15
    • 1970-01-01
    • 2019-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多