Navigation View of Material Design

NavigationView (navigation view) has two parts, the head can load a layout, the menu bar can load a menu file. Generally used in DrawerLayout. NavigationView is provided by the Design Support library, first add dependent libraries.

    compile 'com.android.support:design:24.2.1'
    compile 'de.hdodenhof:circleimageview:2.1.0'

Two dependency libraries are added here, the first is the Design Support library, and the second is an open source control, which is used to display the picture in a circular shape;

Next, write a menu file and a header layout file to load the header and menu bar of NavigationView.

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="single">
        <item
            android:id="@+id/call"
            android:icon="@drawable/nav_call"
            android:title="电话"
            />
        <item
            android:id="@+id/friends"
            android:icon="@drawable/nav_friends"
            android:title="好友"
            />
        <item
            android:id="@+id/location"
            android:icon="@drawable/nav_location"
            android:title="位置"
            />
        <item
            android:id="@+id/mail"
            android:icon="@drawable/nav_mail"
            android:title="邮件"
            />
    </group>
</menu>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="180dp"
    android:background="?attr/colorPrimary"
    android:padding="10dp">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/circleImageView"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="33dp"
        android:src="@drawable/mn" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/circleImageView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="13dp"
        android:text="我在等待,一个有你的未来!"
        android:textColor="#fff"
        android:textSize="12sp" />

</RelativeLayout>

In the menu file, first write a group, and the setting group can only be selected. A CircleImageView (the picture is modified into a circle) and a TextView are placed in the layout file.

Add NavigationView control in the main layout file:

<RelativeLayout 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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.administrator.navigtionviewtest1.MainActivity">

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/nav_menu"></android.support.design.widget.NavigationView>
</RelativeLayout>

app: headerLayout = ”@ layout / nav_header” loads the header view
app: menu = ”@ menu / nav_menu” loads the menu options

Note that xmlns: app = ”http://schemas.android.com/apk/res-auto” adds a new namespace

Next, write logical events in the code:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
        navigationView.setCheckedItem(R.id.call);//设置默认选中项
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            //设置点击事件
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                Toast.makeText(MainActivity.this, "" + item.getTitle(), Toast.LENGTH_SHORT).show();
                return true;
            }
        });
    }
}

Write a picture description here

Published 34 original articles · Like 10 · Visits 30,000+

Guess you like

Origin blog.csdn.net/q296264785/article/details/59484607