Navigation的使用

来源:互联网 发布:人民币贬值 房价 知乎 编辑:程序博客网 时间:2024/06/04 23:31

1.导design包

2.布局

<?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"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:id="@+id/drawer"    tools:context="com.animee.day11.NavigationActivity">    <RelativeLayout        android:id="@+id/content_layout"        android:layout_width="match_parent"        android:layout_height="match_parent">        <TextView            android:id="@+id/navigation_tv"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="我是中间的内容"            android:layout_centerInParent="true"/>    </RelativeLayout>    <android.support.design.widget.NavigationView        android:id="@+id/navigation"        android:layout_width="220dp"        android:layout_height="match_parent"        android:layout_gravity="left"        app:headerLayout="@layout/headlayout"        app:menu="@menu/navigation_menu">    </android.support.design.widget.NavigationView></android.support.v4.widget.DrawerLayout>




3. 然后创建对应的头部布局       app:headerLayout="@layout/headlayout"


<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="200dp"    android:orientation="vertical"    android:background="@color/colorAccent"    android:padding="20dp">    <ImageView        android:id="@+id/img_head"        android:layout_width="match_parent"        android:layout_height="100dp"        android:src="@mipmap/d_aoteman"/>    <TextView        android:id="@+id/tv_head"        android:text="奥特曼"        android:gravity="center"        android:layout_marginTop="20dp"        android:textSize="20sp"        android:layout_width="match_parent"        android:layout_height="wrap_content" /></LinearLayout>





4.创建对应的meun布局    app:menu="@menu/navigation_menu"



<?xml version="1.0" encoding="utf-8"?><menu    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto">    <item app:icon="@android:drawable/ic_menu_add"        android:title="添加"        android:id="@+id/add"/>    <item android:id="@+id/setting"        android:title="设置"        app:icon="@android:drawable/ic_menu_set_as"/>    <item android:id="@+id/delete"        android:title="删除"        app:icon="@android:drawable/ic_menu_delete"/>    <item android:id="@+id/call"        android:title="呼叫"        app:icon="@android:drawable/ic_menu_call"/></menu>





5.主函数

public class NavigationActivity extends AppCompatActivity {    private NavigationView navigationView;    private DrawerLayout drawer;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_navigation);        navigationView = (NavigationView) findViewById(R.id.navigation);        drawer = (DrawerLayout) findViewById(R.id.drawer);//        获取navigationView的头布局        View headerView = navigationView.getHeaderView(0);        ImageView iv = (ImageView) headerView.findViewById(R.id.img_head);        TextView tv = (TextView) headerView.findViewById(R.id.tv_head);//        设置导航栏的点击事件        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {            @Override            public boolean onNavigationItemSelected(MenuItem item) {                switch (item.getItemId()) {                    case R.id.add:                        Toast.makeText(NavigationActivity.this,"您点击了添加按钮",Toast.LENGTH_LONG).show();                        break;                    case R.id.setting:                        Toast.makeText(NavigationActivity.this,"您点击了设置按钮",Toast.LENGTH_LONG).show();                        break;                    case R.id.delete:                        Toast.makeText(NavigationActivity.this,"您点击了删除按钮",Toast.LENGTH_LONG).show();                        break;                    case R.id.call:                        Toast.makeText(NavigationActivity.this,"您点击了呼叫按钮",Toast.LENGTH_LONG).show();                        break;                }                return false;            }        });    }}













原创粉丝点击