DrawerLayout左边导航
2017-03-10 09:15
190 查看
xml文件
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/drawerLayout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="ui.ht.rscja.com.test.MainActivity"> <LinearLayout android:id="@+id/linearLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <EditText android:layout_width="match_parent" android:layout_height="50dp" android:text="111" /> <TextView android:layout_width="match_parent" android:layout_height="200dp" android:text="Hello" android:layout_gravity="bottom" android:textSize="30sp" android:textColor="@color/colorPrimary"/> <FrameLayout android:background="#99F" android:id="@+id/frame_content" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:textSize="20sp" android:text="d"/> </FrameLayout> </LinearLayout> <!--左边抽屉窗--> <android.support.design.widget.NavigationView android:id="@+id/navigation_view" android:layout_height="match_parent" android:layout_width="320dp" android:layout_gravity="start" android:fitsSystemWindows="true" app:headerLayout="@layout/header_layout" app:menu="@menu/menu_left" /> </android.support.v4.widget.DrawerLayout>
fragment_head.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="200dp" > <!-- TODO: Update blank fragment layout --> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/hello_blank_fragment" /> <ImageView android:background="@drawable/home" android:layout_width="match_parent" android:layout_height="match_parent" /> </FrameLayout>menu_left.xml 默认图片是灰色的
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <group> <item android:title="菜单一" android:icon="@drawable/drawable_left_menu_1"></item> <item android:title="菜单二" android:icon="@drawable/drawable_left_menu_2"></item> <item android:title="菜单三" android:icon="@drawable/drawable_left_menu_3"></item> </group> </menu>drawable_left_menu.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/libra_selected" android:state_selected="true"/> <item android:drawable="@drawable/libra"/> </selector>Main.javaprivate NavigationView navigationView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);navigationView=(NavigationView)findViewById(R.id.navigationView);initNavigationView();}//左边抽屉页面private void initNavigationView() {View view =navigationView.getHeaderView(0);view.setOnClickListener(this);navigationView.setNavigationItemSelectedListener(this);navigationView.setItemTextColor(ContextCompat.getColorStateList(this,R.color.drawable_bg_navigtion));//设置字体颜色navigationView.setItemIconTintList(ContextCompat.getColorStateList(this, R.color.drawable_bg_navigtion));//设置菜单图标的颜色setNavigation_viewChecked(0);}private void setNavigation_viewChecked(int id){Menu menu= navigationView.getMenu();for (int k=0;k<menu.size();k++){if(k==id){menu.getItem(k).setChecked(true);}else{menu.getItem(k).setChecked(false);}}}@Overridepublic boolean onNavigationItemSelected(@NonNull MenuItem item) {switch (item.getItemId()){case R.id.menu1:setNavigation_viewChecked(0);break;case R.id.menu2:setNavigation_viewChecked(1);break;case R.id.menu3:setNavigation_viewChecked(2);break;}return false;}res/color/drawable_bg_navigtion.xml 注意:items 几个state的顺序<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_checked="true" android:color="@color/colorAccent"/><item android:state_pressed="true" android:color="@color/colorBackgrund2"/> <!-- pressed颜色 --><item android:state_focused="true" android:color="@color/colorPrimary" /><item android:color="@color/colorBackgrund"/></selector>
相关文章推荐
- 类似 MSDN CSDN 左边导航树效果的实现!
- Action 自定义 左边的箭头导航
- 左边导航的制作(置顶)
- 左边导航树,右面页面的问题
- JQuery定制sharepoint网站的左边导航
- Android左边菜单栏导航
- 类似 MSDN CSDN 左边导航树效果的实现! [JavaScript + ASP]
- 左边多级导航切换右边的main
- 带导航的控制器上面再加个带导航的控制器,但第二个导航只从屏幕左边隔一定距离显示到右边的框
- 类似 MSDN CSDN 左边导航树效果的实现! [JavaScript + ASP]
- android导航抽屉效果使用DrawerLayout
- DrawerLayout的导航图标详解
- Magento左边导航菜单调出方法
- 仿淘宝UED的左边跟随鼠标滑动导航
- myeclipse文件目录自动定位(右编辑界面点击 左边Package Explorer导航自动定位)
- 页面左边导航固定,右边自适应宽度
- 博客园左边导航菜单的问题
- 如何修改织梦后台左边导航栏目
- 用frameset实现左边导航,右边显示相应内容的布局
- html静态实现左边导航右边显示链接页面