1. 程式人生 > 程式設計 >Android studio有關側滑的實現程式碼

Android studio有關側滑的實現程式碼

最近寫課設,因為是新手,實現起來比較麻煩。所以寫下此筆記,免得我以後忘記了。

附圖片:

在這裡插入圖片描述

//主頁面的佈局
activity_main.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: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:

<?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

<?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:

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有關側滑的實現內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!