【问题标题】:Navigation Drawer custom adapter doesn't show images导航抽屉自定义适配器不显示图像
【发布时间】:2014-07-08 12:13:28
【问题描述】:

我无法让我的抽屉显示图像,即使它们被设置在抽屉适配器中。文字显示得很好,所以画出来不是问题,只是设置图标而已。

这是适配器的代码:

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))
        }));

【问题讨论】:

    标签: android imageview navigation-drawer listadapter custom-adapter


    【解决方案1】:

    在设置成setCompoundDrawables之前,做下面这行代码,就可以正常工作了。

    drawable drawable = dp.icon;

    drawable.setBounds(0, 0, 50, 50); //例如,我给了50,根据你的设计你可以改变。

    然后像这样设置为文本视图。

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

    现在它可以正常工作了。

    【讨论】:

    • 不知道这应该如何产生影响,但它确实有效!谢谢!
    • 有没有办法在布局中进行设置?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-04
    相关资源
    最近更新 更多