DrawerNavigationListAdapter带有图标

时间:2015-08-09 22:47:35

标签: java android

我刚刚开始为Android开发,我遇到了问题。我是DrawerNavigationListAdapter和DrawerNavigationListView,并且工作正常,现在我正在尝试添加相应的图标。

这是我的xml:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:padding="8dp" />

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="8dp"
        android:textSize="18sp"
        android:background="@drawable/navigation_list_selector" />

</RelativeLayout>

DrawerNavigationListView.java

public class DrawerNavigationListView extends ListView implements AdapterView.OnItemClickListener {
    public DrawerNavigationListView(Context context) {
        this(context, null);
    }

    public DrawerNavigationListView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DrawerNavigationListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        DrawerNavigationListAdapter adapter = new DrawerNavigationListAdapter( getContext(), 1 );
        adapter.add(getContext().getString(R.string.section_0));
        ...
        setAdapter( adapter );

        setOnItemClickListener( this );
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        EventBus.getInstance().post(new DrawerSectionItemClickedEvent((String) parent.getItemAtPosition(position)));
    }
}

和DrawerNavigationListAdapter

public class DrawerNavigationListAdapter extends ArrayAdapter<String> {

    public DrawerNavigationListAdapter(Context context, int resource) {
        super(context, resource);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if( convertView == null ) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(getContext()).inflate( R.layout.navigation_drawer_list_item, parent, false );
            holder.title = (TextView) convertView.findViewById( R.id.title );
            holder.icon = (ImageView) convertView.findViewById(R.id.icon);
            convertView.setTag( holder );
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.title.setText(getItem(position));

        return convertView;
    }

    class ViewHolder {
        TextView title;
        ImageView icon;
    }
}

我的问题是,如何更改此代码,以便添加图标?

1 个答案:

答案 0 :(得分:1)

在RelativeLayouts中,除非另有说明,否则在其他人之后列出的视图将以更高的z-index绘制。因此,您的TextView正在有效地绘制您的ImageView。你需要使用

android:layout_toRightOf="ImageView" //or layout_toLeftOf

在TextView的xml中或使用

android:drawableLeft="ImageView" //or drawableRight

后者是我在适配器中向TextViews添加图标的方式,但都可以。