• 62、常规控件(5)Navigation View –美观的侧滑视图


    1、main_layout.xml

     1 <android.support.v4.widget.DrawerLayout
     2     xmlns:android="http://schemas.android.com/apk/res/android"
     3     xmlns:app="http://schemas.android.com/apk/res-auto"
     4     android:id="@+id/drawer_layout"
     5     android:layout_width="match_parent"
     6     android:layout_height="match_parent">
     7 
     8     <include layout="@layout/activity_main"/>
    10     <!--
    11         android:fitsSystemWindows="true|false":适合系统窗口
    12     -->
    14     <android.support.design.widget.NavigationView
    15         android:id="@+id/navigetion_view"
    16         android:layout_width="wrap_content"
    17         android:layout_height="match_parent"
    18         android:layout_gravity="left"
    19         android:fitsSystemWindows="true"
    20         app:headerLayout="@layout/head_view"
    21         app:menu="@menu/menu_main">
    24     </android.support.design.widget.NavigationView>
    25 
    26 </android.support.v4.widget.DrawerLayout>

    2、@layout/head_view  侧滑布局文件

     1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     android:layout_width="match_parent"
     3     android:layout_height="match_parent"
     4     android:gravity="center"
     5     android:orientation="vertical">
     6     <ImageView
     7         android:layout_width="wrap_content"
     8         android:layout_height="wrap_content"
     9         android:src="@drawable/ic_launcher"/>
    10 </LinearLayout>

    3、MainActivity.java

     1 package com.example.com.designdemo;
     2 
     3 import android.os.Bundle;
     4 import android.support.design.widget.TabLayout;
     5 import android.support.v4.app.Fragment;
     6 import android.support.v4.view.ViewPager;
     7 import android.support.v7.app.AppCompatActivity;
     9 import java.util.ArrayList;
    10 import java.util.List;
    12 
    13 public class MainActivity extends AppCompatActivity {
    14 
    15     @Override
    16     protected void onCreate(Bundle savedInstanceState) {
    17         super.onCreate(savedInstanceState);
    18         setContentView(R.layout.main_layout);
    19 
    21         TabLayout tabs = (TabLayout) this.findViewById(R.id.tabs);
    22 
    23         List<String> titles = new ArrayList<String>();
    24         List<Fragment> fragments = new ArrayList<Fragment>();
    25         for (int i=0; i<10; i++) {
    26             String title = "Tab"+ (i+1);
    27             tabs.addTab(tabs.newTab().setText(title));
    28             titles.add(title);
    29 
    30             Fragment fragment = new TestOneFragment(title);
    31             fragments.add(fragment);
    32         }
    33 
    34         ViewPager viewpager = (ViewPager) this.findViewById(R.id.viewpager);
    35         TestOneAdapter mAdapter = new TestOneAdapter(getSupportFragmentManager(), titles, fragments);
    36         viewpager.setAdapter(mAdapter);
    37         tabs.setupWithViewPager(viewpager);
    38         tabs.setTabsFromPagerAdapter(mAdapter);
    39 
    40         // 控制侧滑菜单的显示和隐藏
    41         //DrawerLayout drawer_layout = (DrawerLayout) this.findViewById(R.id.drawer_layout);
    42         //drawer_layout.closeDrawer();
    43     }
    44 
    45 }
  • 相关阅读:
    PHP与MongoDB简介|安全|M+PHP应用实例(转)
    在CentOS中安装gcc配置c语言开发环境(转)
    linux svn客户端入门心得(转)
    php socket成功实例
    PHP date函数参数详解(转)
    php socket(fsockopen)的应用实例
    文件字符输出流FileWriter
    TCP(socket)实例
    文件字符输入流FileReader
    卡曼滤波python
  • 原文地址:https://www.cnblogs.com/androidsj/p/4998025.html
Copyright © 2020-2023  润新知