服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android studio有关侧滑的实现代码

Android studio有关侧滑的实现代码

2022-12-29 15:56努力学习中..... Android

这篇文章主要介绍了Android studio有关侧滑的实现代码,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

最近写课设,因为是新手,实现起来比较麻烦。所以写下此笔记,免得我以后忘记了。

附图片:

Android studio有关侧滑的实现代码

//主页面的布局
activity_main.xml:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?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:qpp="http://schemas.android.com/apk/res-auto"
  android:id="@+id/drawer_layout"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:fitsSystemWindows="true"
  tools:openDrawer="start">
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
     />
 
  <android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="207dp"
    android:layout_height="match_parent"
    android:layout_gravity="left"
    android:fitsSystemWindows="true"
    qpp:headerLayout="@layout/stumenu1"
    qpp:menu="@menu/stumenu1" />
 
</android.support.v4.widget.DrawerLayout>

头部的布局(放入layout)
stumenu1.xml:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?xml version="1.0" encoding="utf-8"?>
<!--学生左滑后界面-->
<android.support.constraint.ConstraintLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
 
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    >
 
    <ImageView
      android:id="@+id/person"
      android:layout_width="72dp"
      android:layout_height="72dp"
      android:layout_marginTop="75dp"
      android:src="@drawable/student"
      />
    <TextView
      android:id="@+id/stutv"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:textSize="20sp"
      android:layout_marginTop="20dp"
      android:textColor="#282525"
      android:text="测试APP"/>
 
  </LinearLayout>
 
</android.support.constraint.ConstraintLayout>

菜单的布局:(放在menu文件夹)
stumenu1

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?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">
  <group
    android:checkableBehavior="single">
    <item
      android:id="@+id/result"
      android:icon="@drawable/ic_launcher_background"
      android:checkable="true"
      android:title=" 测试结果"/>
 
  <item
    android:id="@+id/w1"
    android:icon="@drawable/ic_launcher_background"
 
    android:title=" 错题"/>
  <item
    android:id="@+id/s1"
    android:icon="@drawable/ic_launcher_background"
    android:title=" 得分"/>
 
  <item
    android:id="@+id/exit"
    android:icon="@drawable/ic_launcher_background"
    android:title=" 退出"/>
  </group>
</menu>

MainActivity.java:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.example.cholewu.slide;
 
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;
 
public class MainActivity extends AppCompatActivity {
 
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
 
    //左滑菜单
    initView();
  }
  private void initView() {
 
    //实现左右滑动
    final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    //菜单控件
    final NavigationView nv = findViewById(R.id.nav_view);
    nv.setItemIconTintList(null);
    
    nv.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
      @Override
      public boolean onNavigationItemSelected(@NonNull MenuItem item) {
 
        switch (item.getItemId()){
          case R.id.exit:
            //跳转到退出页面
            Toast.makeText(MainActivity.this,"你已退出登录!",Toast.LENGTH_SHORT).show();
            Intent intent=new Intent();
            intent.setClass(MainActivity.this,Exit.class);
            startActivity(intent);
            item.setChecked(true);
            break;
        }
 
        item.setCheckable(true);//设置可选项
        item.setChecked(true);//设置被选中
        drawer.closeDrawers();//关闭菜单栏
        return true;
      }
 
    });
  }
}

(注意:如果直接复制代码的话,android.support.design.widget.NavigationView可能会出错,需要自己在design那里布局,如果出错,可以看看以下NavigationView右边是否有下载图案,点击下载就行了)

Android studio有关侧滑的实现代码

总结

到此这篇关于Android studio有关侧滑的实现的文章就介绍到这了,更多相关Android studio有关侧滑的实现内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/qq_46601555/article/details/106481093

延伸 · 阅读

精彩推荐
  • AndroidAndroid触摸事件如何实现笔触画布详解

    Android触摸事件如何实现笔触画布详解

    这篇文章主要给大家介绍了关于Android触摸事件如何实现笔触画布的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学...

    张风捷特烈11502022-08-15
  • AndroidAndroid实现简易计算器小程序

    Android实现简易计算器小程序

    这篇文章主要为大家详细介绍了Android实现简易计算器小程序,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    Vivinia_Vivinia8352022-10-19
  • AndroidAndroid闪屏效果实现方法

    Android闪屏效果实现方法

    这篇文章主要介绍了Android闪屏效果实现方法,结合实例形式分析了Android闪屏效果的实现原理及相关功能与布局设置技巧,需要的朋友可以参考下...

    一剪梅7082021-05-18
  • AndroidAndroid如何帮助用户自动接听或者挂断来电

    Android如何帮助用户自动接听或者挂断来电

    这篇文章主要为大家详细介绍了Android帮助用户自动接听或者挂断来电,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    十个雨点9742022-02-12
  • AndroidAndroid控件CardView实现卡片布局

    Android控件CardView实现卡片布局

    这篇文章主要为大家详细介绍了Android控件CardView实现卡片布局,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    简单不一定不好4732022-08-11
  • AndroidAndroid Notification通知解析

    Android Notification通知解析

    这篇文章主要针对Android Notification通知进行解析,本文主要介绍的是notification通知的使用方法,感兴趣的小伙伴们可以参考一下...

    jumtre5442021-05-19
  • Androidandroid绘制触点轨迹的代码

    android绘制触点轨迹的代码

    这篇文章主要为大家详细介绍了android绘制触点轨迹的相关代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    二仪式3492022-10-19
  • AndroidAndroid开发中Intent.Action各种常见的作用汇总

    Android开发中Intent.Action各种常见的作用汇总

    今天小编就为大家分享一篇关于Android开发中Intent.Action各种常见的作用汇总,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友...

    franksight11842022-09-01