
如何在NavigationVIEw中添加自定义布局并设计我的创建自定义NavigationVIEw使用材质设计,我想把我的抽屉图标放到右边,文本左边是这样的
解决方法:
我搜索太多,这是我的经验,工作正常
首先为标题创建布局.它的名字是nav_header_main.xml,然后把它放在res的布局文件夹中,并将此代码放入其中.
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="@dimen/nav_header_height" androID:background="@drawable/header" androID:paddingBottom="@dimen/activity_vertical_margin" androID:paddingleft="@dimen/activity_horizontal_margin" androID:paddingRight="@dimen/activity_horizontal_margin" androID:paddingtop="@dimen/activity_vertical_margin" androID:theme="@style/themeOverlay.AppCompat.Dark" androID:gravity="top"> <relativeLayout androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:padding="16dp"> <de.hdodenhof.circleimagevIEw.circleimageVIEw androID:ID="@+ID/cv_nave_profile_image" androID:layout_wIDth="@dimen/nav_profile_image" androID:layout_height="@dimen/nav_profile_image" androID:layout_alignParentRight="true" androID:layout_alignParenttop="true" androID:src="@drawable/profile" /> <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_toleftOf="@ID/cv_nave_profile_image" androID:layout_alignParenttop="true" androID:padding="@dimen/activity_horizontal_margin" androID:orIEntation="vertical" > <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:ID="@+ID/tv_nav_name" androID:textStyle="bold" androID:typeface="sans" androID:textcolor="#ffffff" androID:gravity="right" androID:layout_gravity="right" androID:text="رخداد جدید" androID:paddingBottom="5dp" androID:textSize="@dimen/body" /> </linearLayout> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignParentBottom="true" androID:typeface="sans" androID:textcolor="#ffffff" androID:ID="@+ID/tv_nav_phone" androID:layout_alignParentleft="true" androID:text="0370077315" /> </relativeLayout></linearLayout>然后我把它包含在NavigationVIEw的子项中,对于菜单项我使用RecyclerVIEw来显示菜单和图标,这样我的NavigationVIEw就是
<androID.support.design.Widget.CoordinatorLayout androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:fitsSystemwindows="true" tools:context="spydroID.ir.dorobar.ActivitIEs.SearchActivity"> <androID.support.design.Widget.AppbarLayout androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:theme="@style/Apptheme.AppbarOverlay"> <androID.support.v7.Widget.Toolbar androID:ID="@+ID/toolbar" androID:layout_wIDth="match_parent" androID:layout_height="?attr/actionbarSize" androID:background="?attr/colorPrimary" app:popuptheme="@style/Apptheme.PopupOverlay"> </androID.support.v7.Widget.Toolbar> </androID.support.design.Widget.AppbarLayout> <include layout="@layout/content_search" /> <androID.support.design.Widget.floatingActionbutton androID:ID="@+ID/fab" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_gravity="bottom|end" androID:layout_margin="@dimen/fab_margin" androID:src="@androID:drawable/ic_dialog_email" /></androID.support.design.Widget.CoordinatorLayout><androID.support.design.Widget.NavigationVIEw androID:ID="@+ID/nav_vIEw" androID:layout_wIDth="fill_parent" androID:layout_height="match_parent" androID:layout_gravity="right" androID:fitsSystemwindows="true" androID:layout_marginleft="@dimen/nav_margin" > <linearLayout androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" androID:orIEntation="vertical" > <include layout="@layout/nav_header_main" /> <relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent"> <androID.support.v7.Widget.RecyclerVIEw androID:ID="@+ID/drawer_slIDermenu" androID:layout_wIDth="fill_parent" androID:layout_height="match_parent" androID:layout_margintop="16dp"/> </relativeLayout> </linearLayout></androID.support.design.Widget.NavigationVIEw>只需要记住将NavigationVIEw放在DrawerLayout中
然后我用ImageVIEw和TextVIEw为这个布局创建菜单项的布局,这个名字是card_drawer_item.xml,它的代码在这里
<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="48dp"> <ImageVIEw androID:ID="@+ID/drawer_icon" androID:layout_wIDth="25dp" androID:layout_height="wrap_content" androID:layout_alignParentRight="true" androID:layout_marginleft="12dp" androID:layout_marginRight="12dp" androID:src="@drawable/ic_about" androID:layout_centerVertical="true" /> <TextVIEw androID:ID="@+ID/drawer_text" androID:layout_wIDth="wrap_content" androID:layout_height="match_parent" androID:layout_toleftOf="@ID/drawer_icon" androID:minHeight="?androID:attr/ListPreferredItemHeightSmall" androID:textAppearance="?androID:attr/textAppearanceListItemSmall" androID:gravity="center_vertical" androID:typeface="sans" androID:paddingRight="40dp"/></relativeLayout>然后我为这个布局创建VIEwHolder文件夹.
public class DrawerItemHolder extends RecyclerVIEw.VIEwHolder { public ImageVIEw itemIcon; public TextVIEw itemText; public DrawerItemHolder(VIEw itemVIEw) { super(itemVIEw); itemIcon= (ImageVIEw) itemVIEw.findVIEwByID(R.ID.drawer_icon); itemText= (TextVIEw) itemVIEw.findVIEwByID(R.ID.drawer_text); }}现在我将菜单项的文本定义为字符串数组,并在strings.xml中的菜单中包含菜单图标
<string-array name="drawer_items"> <item>setting</item> <item>add record</item> <item>ads</item> <item>about</item> <item>call</item> <item>help</item> <item>privacy</item> </string-array> <array name="drawers_icons"> <item>@drawable/ic_action_settings</item> <item>@drawable/ic_plus</item> <item>@drawable/ic_ads</item> <item>@drawable/ic_about</item> <item>@drawable/ic_phone</item> <item>@drawable/ic_help</item> <item>@drawable/ic_policy</item> </array>那么我们只需要这样的适配器
public class DrawerItemAdapter extends RecyclerVIEw.Adapter<DrawerItemHolder> { // slIDe menu items private List<DrawerItem> items; private List<Integer> drawerIcons; public DrawerItemAdapter(List<DrawerItem> items) { super(); this.items = items; } @OverrIDe public DrawerItemHolder onCreateVIEwHolder(VIEwGroup parent, int vIEwType) { VIEw itemVIEw = LayoutInflater. from(parent.getContext()). inflate(R.layout.card_drawer_item, parent, false); return new DrawerItemHolder(itemVIEw); } @OverrIDe public voID onBindVIEwHolder(DrawerItemHolder holder, int position) { holder.itemIcon.setimageResource(items.get(position).getIconID()); holder.itemText.setText(items.get(position).getText()); } @OverrIDe public int getItemCount() { return items.size(); }}一切都很好..刚才我们必须在Activity中设置NavigationVIEw.
protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_search); Toolbar toolbar = (Toolbar) findVIEwByID(R.ID.toolbar); setSupportActionbar(toolbar); drawer = (DrawerLayout) findVIEwByID(R.ID.drawer_layout); recList = (RecyclerVIEw) findVIEwByID(R.ID.drawer_slIDermenu); recList.setHasFixedSize(true); linearlayoutmanager llm = new linearlayoutmanager(this); llm.setorIEntation(linearlayoutmanager.VERTICAL); recList.setLayoutManager(llm); String []itemsTitle=getResources().getStringArray(R.array.drawer_items); TypedArray icons=getResources().obtainTypedArray(R.array.drawers_icons); List<DrawerItem>drawerItems= new ArrayList<DrawerItem>(); for(int i=0;i<itemsTitle.length;i++){ drawerItems.add(new DrawerItem(icons.getResourceID(i,-1),itemsTitle[i])); } DrawerItemAdapter ad= new DrawerItemAdapter(drawerItems); recList.setAdapter(ad); } @OverrIDe public voID onBackpressed() { if (drawer.isDrawerOpen(GravityCompat.END)) { drawer.closeDrawer(GravityCompat.END); return; } super.onBackpressed(); } 总结 以上是内存溢出为你收集整理的android – 使用带有标题和列表的NavigationDrawer中的自定义布局全部内容,希望文章能够帮你解决android – 使用带有标题和列表的NavigationDrawer中的自定义布局所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)