我无法让我的抽屉显示图像,即使它们已设置在抽屉适配器中。它显示的文本很好,所以这不是全部绘制的问题,只是设置图标。
这是适配器的代码:
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/