如何在NavigationView中添加自定义布局并设计我的创建自定义NavigationView使用材质设计,我想把我的抽屉图标放到右边,文本左边是这样的
解决方法:
我搜索太多,这是我的经验,工作正常
首先为标题创建布局.它的名字是nav_header_main.xml,然后把它放在res的布局文件夹中,并将此代码放入其中.
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">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
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"
/>
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"
>
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"
/>
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"
/>
然后我把它包含在NavigationView的子项中,对于菜单项我使用RecyclerView来显示菜单和图标,这样我的NavigationView就是
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="spydroid.ir.dorobar.Activities.SearchActivity">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay">
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:layout_width="fill_parent" android:layout_height="match_parent"
android:layout_gravity="right" android:fitsSystemWindows="true"
android:layout_marginLeft="@dimen/nav_margin"
>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
android:layout_width="match_parent"
android:layout_height="match_parent">
android:id="@+id/drawer_slidermenu"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginTop="16dp"/>
只需要记住将NavigationView放在DrawerLayout中
然后我用ImageView和TextView为这个布局创建菜单项的布局,这个名字是card_drawer_item.xml,它的代码在这里
android:layout_width="match_parent"
android:layout_height="48dp">
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" />
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"/>
然后我为这个布局创建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中的菜单中包含菜单图标
setting
add record
ads
about
call
help
privacy
@drawable/ic_action_settings
@drawable/ic_plus
@drawable/ic_ads
@drawable/ic_about
@drawable/ic_phone
@drawable/ic_help
@drawable/ic_policy
那么我们只需要这样的适配器
public class DrawerItemAdapter extends RecyclerView.Adapter {
// slide menu items
private List items;
private List drawerIcons;
public DrawerItemAdapter(List 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);
ListdrawerItems= new ArrayList();
for(int i=0;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();
}