我的Drawerlayout gridview显示如列表视图

时间:2015-04-04 15:17:31

标签: android gridview drawerlayout

我使用抽屉布局在网格视图中设置图像,但它在每一行显示为列表。我想在网格视图中显示图像。如何做?我可以做或者除了抽屉布局之外还应该做什么? 我的抽屉布局:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

    </FrameLayout>

    <GridView
        android:id="@+id/list_slidermenu"
        android:layout_width="240dp"
        android:layout_height="fill_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@color/list_divider"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_selector"
        android:background="@color/list_background"
        />
</android.support.v4.widget.DrawerLayout>

网格视图项目:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/list_selector">
        <ImageView
            android:id="@+id/icon"
            android:layout_width="60dp"
            android:layout_height="60dp"/>
</LinearLayout>

和适配器:

public class NavDrawerListAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<NavDrawerItem> navDrawerItems;

    public NavDrawerListAdapter(Context context, ArrayList<NavDrawerItem> navDrawerItems){
        this.context = context;
        this.navDrawerItems = navDrawerItems;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater mInflater = (LayoutInflater)
                    context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.list_menu, null);
        }
        ImageView imgIcon = (ImageView) convertView.findViewById(R.id.icon);

        imgIcon.setImageResource(navDrawerItems.get(position).getIcon());


        return convertView;
    }
}

2 个答案:

答案 0 :(得分:3)

您需要定义所需的列数,

        android:numColumns="4" //column amount you need

答案 1 :(得分:0)

试试这个

<GridView
android:layout_width="match_parent"
android:layout_height="340dp"
android:id="@+id/gridView1"
android:layout_weight="1"
android:layout_gravity="top"
android:stretchMode="columnWidth"
android:numColumns="4"/>

是的,您必须根据需要添加numColumns