側滑頁面
阿新 • • 發佈:2019-01-12
佈局頁面
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical" tools:context=".MainActivity"> <android.support.v4.widget.DrawerLayout android:id="@+id/draw" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000000" android:orientation="horizontal"> <Button android:id="@+id/butt_01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="點選" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="left" android:background="#ded3d3" android:orientation="horizontal"> <Button android:id="@+id/butt_02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="單擊" /> </LinearLayout> </android.support.v4.widget.DrawerLayout> </LinearLayout>
MainActivity
package com.example.demo_drawerlayot; import android.support.annotation.NonNull; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.widget.GridView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //獲取控制元件 DrawerLayout drawerLayout = findViewById(R.id.draw); //點選主頁面的按鈕彈出側滑頁面 drawerLayout.openDrawer(Gravity.LEFT); //點選側滑頁面關閉側滑頁面 drawerLayout.closeDrawer(Gravity.LEFT); //側滑監聽 drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() { @Override public void onDrawerSlide(@NonNull View drawerView, float slideOffset) { } @Override public void onDrawerOpened(@NonNull View drawerView) { } @Override public void onDrawerClosed(@NonNull View drawerView) { } @Override public void onDrawerStateChanged(int newState) { } }); } }
效果圖