你可以像我所做的那样在浏览视图中使用listview.这样做的好处是可以更好地控制这一点:
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/header"
>
android:id="@+id/navigationmenu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="192dp"
android:background="@android:color/white">
nav项目应该是这样的:
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/cardview_dark_background"
>
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_height="wrap_content"
android:src="@drawable/viewlist"
/>
android:id="@+id/tvlstName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:layout_marginRight="30dp"
android:textColor="@color/colorPrimary"
/>
和navcustomadapter这样:
package com.example.android.dezcook;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
/**
* Created by Android on 6/4/2016.
*/
public class navCustomAdapter extends BaseAdapter {
String[] result;
Context context;
int[] imageId;
private static LayoutInflater inflater=null;
public navCustomAdapter(MainActivity mainActivity,String[] prgmNameList,int[] prgImages)
{
result=prgmNameList;
imageId=prgImages;
context =mainActivity;
inflater =(LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount()
{
return result.length;
}
@Override
public Object getItem(int position)
{
return position;
}
@Override
public long getItemId(int position)
{
return position;
}
public class Holder
{
TextView tv;
ImageView img;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
Holder holder=new Holder();
View rowView;
rowView=inflater.inflate(R.layout.navitem,null);
holder.tv=(TextView)rowView.findViewById(R.id.tvlstName);
holder.img=(ImageView)rowView.findViewById(R.id.img);
holder.tv.setText(result[position]);
holder.img.setImageResource(imageId[position]);
rowView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context,"you Clicked "+result[position],Toast.LENGTH_LONG).show();
}
}
);
return rowView;
}
}
最终在主要活动中称之为:
ListView lv=(ListView)findViewById(R.id.navigationmenu);
lv.setAdapter(new navCustomAdapter(this,navItem,prgmImages)