2018/10/31

[Android] Drawer

本篇寫 Android App 的抽屜物件(Drawer),從左邊滑出,如下圖效果。
 

首先在 app 的 build.gradle 裡面的 dependencies 加入這行:
  1. implementation 'com.android.support:design:26.1.0'

在 res/layout 資料夾裡面建立一個 nav_header_main.xml:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.constraint.ConstraintLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:app="http://schemas.android.com/apk/res-auto"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent">
  7.  
  8. <TextView
  9. android:id="@+id/id_tvTitle01"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:text="Title 1"
  13. android:textSize="80px"
  14. android:onClick="onClickDrawer"/>
  15.  
  16. <TextView
  17. android:id="@+id/id_tvTitle02"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:layout_marginTop="8dp"
  21. android:onClick="onClickDrawer"
  22. android:text="Title 2"
  23. android:textSize="80px"
  24. app:layout_constraintTop_toBottomOf="@+id/id_tvTitle01" />
  25.  
  26. </android.support.constraint.ConstraintLayout>
這個 xml 主要來設計 Drawer 的物件,在這裡裡面的 TextView 可以設定 onClick 的按鈕事件來做事情。

接著再建立一個 drawer_main.xml:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:id="@+id/drawer_layout"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. tools:context="com.example.chris.myapplication.MainActivity">
  9.  
  10. <include
  11. layout="@layout/activity_main"
  12. android:layout_width="match_parent"
  13. android:layout_height="match_parent" />
  14.  
  15. <android.support.design.widget.NavigationView
  16. android:id="@+id/nav_view"
  17. android:layout_width="400px"
  18. android:layout_height="1845px"
  19. android:layout_gravity="start"
  20. android:fitsSystemWindows="true"
  21. app:headerLayout="@layout/nav_header_main" />
  22.  
  23. </android.support.v4.widget.DrawerLayout>
在這個 xml 裡面要把原本主要的 activity_main 這個layout 包含進來

最後是 activity_main.xml:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context="com.example.chris.myapplication.MainActivity">
  8.  
  9. <TextView
  10. android:id="@+id/textView"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:layout_marginEnd="8dp"
  14. android:layout_marginStart="8dp"
  15. android:layout_marginTop="8dp"
  16. android:text="MainActivity"
  17. app:layout_constraintEnd_toEndOf="parent"
  18. app:layout_constraintStart_toStartOf="parent"
  19. app:layout_constraintTop_toTopOf="parent" />
  20.  
  21. <Button
  22. android:id="@+id/button"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:layout_marginEnd="8dp"
  26. android:layout_marginStart="8dp"
  27. android:layout_marginTop="8dp"
  28. android:text="Open Drawer"
  29. app:layout_constraintEnd_toEndOf="parent"
  30. app:layout_constraintStart_toStartOf="parent"
  31. app:layout_constraintTop_toBottomOf="@+id/textView"
  32. android:onClick="onClickBtn"/>
  33.  
  34. </android.support.constraint.ConstraintLayout>

程式碼的部分 MainActivity.java:
  1. public class MainActivity extends AppCompatActivity{
  2.  
  3. private String TAG = "MainActivity";
  4.  
  5. private DrawerLayout drawer;
  6.  
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.drawer_main);
  11.  
  12. drawer = findViewById(R.id.drawer_layout);
  13. }
  14.  
  15. public void onClickBtn(View v){
  16. drawer.openDrawer(GravityCompat.START);
  17. }
  18.  
  19. public void onClickDrawer(View v){
  20. switch (v.getId()){
  21. case R.id.id_tvTitle01:
  22. Log.d(TAG, "onClick Title 1");
  23. break;
  24.  
  25. case R.id.id_tvTitle02:
  26. Log.d(TAG, "onClick Title 2");
  27. break;
  28. }
  29. }
  30. }


END

沒有留言:

張貼留言