布局
NavigationView:在Material Design中,Navigation drawer导航抽屉,被设计用于应用导航,提供了一种通用的导航方式,体现了设计的一致性。
而NavigationView的典型用途就是配合之前v4包的DrawerLayout,作为其中的Drawer部分,即导航菜单的本体部分。NavigationView是一个导航菜单框架,使用menu资源填充数据,使我们可以更简单高效的实现导航菜单。它提供了不错的默认样式、选中项高亮、分组单选、分组子标题、以及可选的Header。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent">
<FrameLayout android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent"></FrameLayout>
<android.support.design.widget.NavigationView android:id="@+id/nav_view" android:layout_width="150dp" android:layout_height="match_parent" android:layout_gravity="start" android:background="#303030" app:headerLayout="@layout/nav_header" app:itemIconTint="@color/white" app:itemTextColor="@color/white" app:menu="@menu/nav_menu" />
</android.support.v4.widget.DrawerLayout>
|
注意其中NavigationView的两个自定义属性
app:headerLayout接收一个layout,作为导航菜单顶部的Header,可选项。
app:menu接收一个menu,作为导航菜单的菜单项,几乎是必选项,不然这个控件就失去意义了。但也可以在运行时动态改变menu属性。
用于NavigationView的典型menu文件,应该是一个可选中菜单项的集合。其中checked=”true”的item将会高亮显示,这可以确保用户知道当前选中的菜单项是哪个。item的选中状态可以在代码中设置,代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <group android:id="@+id/g1" android:checkableBehavior="single"> <item android:id="@+id/nav_queue" android:checkable="true" android:icon="@mipmap/queue" android:title="排队" /> <item android:id="@+id/nav_count" android:checkable="true" android:icon="@mipmap/count" android:title="统计" /> <item android:id="@+id/nav_tv" android:checkable="true" android:icon="@mipmap/tv" android:title="TV设置" /> <item android:id="@+id/nav_more" android:checkable="true" android:icon="@mipmap/more" android:title="更多" /> </group> </menu>
|
FragmentActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| public class HomeActivity extends FragmentActivity implements NavigationView .OnNavigationItemSelectedListener {
public static NavigationView navView; public static DrawerLayout drawerLayout;
private QueueFragment queueFragment; private MoreFragment moreFragment; private CountFragment countFragment; private TVFragment tvFragment; private FragmentManager fm;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate( savedInstanceState ); setContentView( R.layout.activity_home );
navView = (NavigationView) findViewById( R.id.nav_view ); drawerLayout = (DrawerLayout) findViewById( R.id.drawer_layout );
init(); }
public void init() { fm = getSupportFragmentManager(); queueFragment = new QueueFragment(); FragmentTransaction ft = fm.beginTransaction(); ft.add( R.id.content, queueFragment, "queue" ).commit();
navView.setNavigationItemSelectedListener( this ); }
public static void showDrawerLayout() { if (!HomeActivity.drawerLayout.isDrawerOpen( HomeActivity.navView )) { HomeActivity.drawerLayout.openDrawer( HomeActivity.navView ); } }
@Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { FragmentTransaction ft = fm.beginTransaction(); hideFragment( ft ); switch (item.getItemId()) { case R.id.nav_queue: if (queueFragment == null) { queueFragment = new QueueFragment(); ft.add( R.id.content, queueFragment, "queue" ); } else { ft.show( queueFragment ); } break; case R.id.nav_count: if (countFragment == null) { countFragment = new CountFragment(); ft.add( R.id.content, countFragment, "count" ); } else { ft.show( countFragment ); } break; case R.id.nav_tv: if (tvFragment == null) { tvFragment = new TVFragment(); ft.add( R.id.content, tvFragment, "tv" ); } else { ft.show( tvFragment ); } break; case R.id.nav_more: if (moreFragment == null) { moreFragment = new MoreFragment(); ft.add( R.id.content, moreFragment, "more" ); } else { ft.show( moreFragment ); } break; } ft.commit(); drawerLayout.closeDrawers();
return true; }
public void hideFragment(FragmentTransaction ft) { if (queueFragment != null) { ft.hide( queueFragment ); } if (moreFragment != null) { ft.hide( moreFragment ); } if (countFragment != null) { ft.hide( countFragment ); } if (tvFragment != null) { ft.hide( tvFragment ); } } }
|
这是其中一个子Fragment的例子,其余的都一样
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| package com.mxt.net.protect.UI.fragment;
import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup;
import com.mxt.net.protect.R;
public class TVFragment extends Fragment { private View view;
@Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { view = inflater.inflate(R.layout.tv_fragment, container, false); return view; } }
|
参考:https://blog.csdn.net/u010068253/article/details/70155261