DrawerLayout左边导航

本文介绍了一个使用Android Support Library实现的导航抽屉(Navigation Drawer)布局案例,详细展示了如何通过XML定义界面元素及交互逻辑,并在Java代码中初始化NavigationView组件,实现左侧菜单项的选择效果。

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.java

 private NavigationView navigationView;
    @Override
    protected 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);
            }
        }
    }
    @Override
    public 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>




评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值