android - 抽屉导航自定义适配器不显示图像

标签 android imageview navigation-drawer listadapter custom-adapter

我无法让我的抽屉显示图像,即使它们已设置在抽屉适配器中。它显示的文本很好,所以这不是全部绘制的问题,只是设置图标。

这是适配器的代码:

public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if(v==null){
        v=LayoutInflater.from(getContext()).inflate(R.layout.drawer_item, null);
    }
    DrawerPair dp = getItem(position);
    TextView description = (TextView) v.findViewById(R.id.drawer_description);
    if(description!=null){
        description.setText(dp.title);
        description.setCompoundDrawables(dp.icon, null, null, null);
    }
    return v;
}

布局如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:gravity="right"
    android:layout_height="match_parent"
    android:orientation="horizontal" >


    <TextView 
        android:id="@+id/drawer_description"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>
</LinearLayout>

这就是它的设置方式。图标是png图像:

mDrawerListView.setAdapter(new DrawerAdapter(
        getActionBar().getThemedContext(),
        R.layout.drawer_item,
        new DrawerPair[]{
                new DrawerPair("title1",getResources().getDrawable(R.drawable.icon1)),
                new DrawerPair("title2",getResources().getDrawable(R.drawable.icon2)),
                new DrawerPair("title3",getResources().getDrawable(R.drawable.icon3))
        }));

最佳答案

在设置 setCompoundDrawables 之前,执行以下代码行,它会正常工作。

Drawable drawable = dp.icon;

drawable.setBounds(0, 0, 50, 50);//例如,我给了 50,您可以根据自己的设计进行更改。

然后像这样设置为 TextView 。

description.setCompoundDrawables(drawable, null, null, null);

现在它可以正常工作了。

关于android - 抽屉导航自定义适配器不显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24631611/

相关文章:

ios - Imageviewframe.origin.xframe.origin.y总是返回0,0

java - 如何控制 Activity 的宽度或可见性

Android:如何以编程方式直接调用 PIN 屏幕?

Android Camera2 手动闪光并不总是有效

android - AsyncTask Android ProgressBar 不显示 [可能重复]

java - 是否可以使用 Android 打开多个蓝牙套接字到单个设备?

android - 未单击图像的嵌套 Recyclerview

android - Xamarin, ImageView 问题

android - Android 操作栏上的左菜单图标

android - 来自 xml 的 NavigationView onClick 导致错误