您的位置:首页 > 其它

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>

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: