Computer >> Máy Tính >  >> Lập trình >> Android

Làm cách nào để tạo ngăn điều hướng tùy chỉnh trong Android?

Ví dụ này minh họa về Cách thay đổi kích thước Hình ảnh trong Ứng dụng Android.

Bước 1 - Tạo một dự án mới trong Android Studio, đi tới Tệp ⇒ Dự án Mới và điền tất cả các chi tiết cần thiết để tạo một dự án mới.

Bước 2 - Thêm mã sau vào res / layout / activity_main.xml.

      

Bước 3 - Thêm mã sau vào res / layout / nav_header_main.xml.

       

Bước 4 - Thêm mã sau vào res / layout / app_bar_main.xml.

         

Bước 5 - Thêm mã sau vào res / layout / content_main.xml.

    

Bước 6 - Thêm mã sau vào res / menu / activity_main_drawer.xml.

        
         

Bước 7 - Thêm mã sau vào src / MainActivity.java

 gói app.tutorialspoint.com.sample; nhập android.os.Bundle; nhập android.support.annotation. NonNull; nhập android.support.design.widget.FloatingActionButton; nhập android.support.design.widget.Snackbar; nhập android.view.View; nhập android.support.design.widget.NavigationView; nhập android.support.v4.view .GravityCompat; nhập android.support.v4.widget.DrawerLayout; nhập android.support.v7.app.ActionBarDrawerToggle; nhập android.support.v7.app.AppCompatActivity; nhập android.support.v7.widget.Toolbar; nhập android. view.Menu; import android.view.MenuItem; public class MainActivity mở rộng AppCompatActivityimplements NavigationView.OnNavigationItemSelectedListener {@Override được bảo vệ void onCreate (Gói đã lưuInstanceState) {super .onCreate (saveInstanceState); setContentView (R.layout. activity_main); Toolbar toolbar =findViewById (thanh công cụ R.id.); setSupportActionBar (thanh công cụ); FloatingActionButton fab =findViewById (R.id. Fab); fab.setOnClickListener (new View.OnClickListener () {@Override public void onClick (View view) {Snackbar. make (view, "Replace with your own action", Snackbar. LENGTH_LONG) .setAction ("Action", null) .show ();}}); DrawerLayout ngăn =findViewById (R.id. Ngăn_layout); ActionBarDrawerToggle toggle =new ActionBarDrawerToggle (cái này, ngăn kéo, thanh công cụ, R.string. Navigation_drawer_open, R.string. Navigation_drawer_close); ngăn kéo.addDrawerListener (chuyển đổi); toggle.syncState (); NavigationView navigationView =findViewById (R.id. Nav_view); navigationView.setNavigationItemSelectedListener (this); } @Override public void onBackPressed () {DrawerLayout Drawer =findViewById (R.id. Draw_layout); if (ngăn kéo.isDrawerOpen (GravityCompat. START)) {ngăn kéo.closeDrawer (GravityCompat. START); } else {super .onBackPressed (); }} @Override public boolean onCreateOptionsMenu (Menu menu) {// Tăng menu; điều này sẽ thêm các mục vào thanh tác vụ nếu nó hiện diện. getMenuInflater (). phồng lên (R.menu. main, menu); trả về true; } @Override public boolean onOptionsItemSelected (MenuItem item) {int id =item.getItemId (); if (id ==R.id. action_settings) {return true; } trả về super .onOptionsItemSelected (item); } @SuppressWarnings ("StatementWithEmptyBody") @Override public boolean onNavigationItemSelected (@NonNull MenuItem item) {// Xử lý các lần nhấp vào mục trong chế độ xem điều hướng tại đây. int id =item.getItemId (); if (id ==R.id. nav_camera) {// Xử lý hành động của camera} else if (id ==R.id. nav_gallery) {} else if (id ==R.id. nav_slideshow) {} else if ( id ==R.id. nav_manage) {} else if (id ==R.id. nav_share) {} else if (id ==R.id. nav_send) {} ​​DrawerLayout ngăn =findViewById (R.id. draw_layout); Drawer.closeDrawer (GravityCompat. START); trả về true; }} 

Bước 8 - Thêm mã sau vào androidManifest.xml

   <ứng dụng android:allowBackup ="true" android:icon ="@ mipmap / ic_launcher" android:label ="@ string / app_name" android:roundIcon ="@ mipmap / ic_launcher_round" android:supportsRtl ="true" android:theme ="@ style / AppTheme">         

Hãy thử chạy ứng dụng của bạn. Tôi giả sử bạn đã kết nối thiết bị Di động Android thực tế với máy tính của mình. Để chạy ứng dụng từ android studio, hãy mở một trong các tệp hoạt động của dự án của bạn và nhấp vào biểu tượng Chạy từ thanh công cụ. Chọn thiết bị di động của bạn làm tùy chọn, sau đó kiểm tra thiết bị di động sẽ hiển thị màn hình mặc định của bạn -

Làm cách nào để tạo ngăn điều hướng tùy chỉnh trong Android?