Faol paneli/asboblar paneli uchun qaytarish tugmasi qanday qo'shiladi

Ish tugmachasini/asboblar panelidagi tugma tugmasini bosish kerak bo'lgan dasturning bir qismini yozdim va tugma bosilganda, oldingi sahifa (sahifa/fragment mavjud sahifadan/fragmandan darhol oldin) ) ko'rsatiladi.

Ushbu kod ToolBar, DrawerLayout, NavigationView va getSupportActionBar ():

final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
final NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);

android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setVisibility(View.VISIBLE);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setLogo(R.mipmap.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);

ActionBar-dan foydalana olmayapman. Nima uchun (nima uchun bilmayman), mening Android studio/dasturim, men ActionBar'dan foydalanishga ruxsat bermaydi. Shunday qilib, men set/getSupportActionBar() bilan ish tutaman.

Bunga bog'liq bo'lgan funksiya quyidagilardir:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
     MenuInflater inflater = getMenuInflater();
     inflater.inflate(R.menu.menu_settings, menu);
     return true;
}        

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    //Handle action bar item clicks here. The action bar will
    //automatically handle clicks on the Home/Up button, so long
    //as you specify a parent activity in AndroidManifest.xml.

     int id = item.getItemId();

     switch (id) {
         case android.R.id.home:
             onBackPressed();
             return true;
         default:
             return super.onOptionsItemSelected(item);
         }
    }        

@Override
public void onBackPressed() {

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();        
    }
}

My activity_main.xml fayli:

<?xml version="1.0" encoding="utf-8"?>


    

    

    

        

        

            


        

    
    

Muammo shundaki, foydali kod nimani anglatishini bilmayman, ya'ni foydasiz kod va ushbu kodlarni (kodlar, usullar, o'zgaruvchilar/moslamalarni, fragmentlar, xml formatlari) olish kerakli natija, ya'ni ish satrida/asboblar panelidagi orqa tugmani qo'llash.

Yuqoridagi kodning aksariyati yuqoriga tugmasi uchun emas, orqaga tugmasi uchun amalga oshiriladi. Men yuqorida va orqada tugmalar bir xil emasligini bir necha joyda o'qib chiqdim.

Men internetda va saytda bir nechta havolalarni sinab ko'rdim, ammo ularning hech biri menga kerakli narsaga ega emas.

Umid qilamanki kimdir menga aniq javob berishi mumkin ...

1

6 javoblar

Orqa belgini ToolBar -ga qo'shishingiz mumkin:

ToolBar dasturini ishga tushiring:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
  • You can use an drawable icon as a back button.

    toolbar.setNavigationIcon(R.drawable.your_drawable_icon);
    
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           //what do you want here
        }
    });
    
  • If you do not want to use drawable icon then:

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           //what do you want here
        }
    });
    
5
qo'shib qo'ydi

Aslida bu tartibga ega bo'lgan tartib sizda RelativeLayout da asboblar paneli qo'shilgandan so'ng, tortmasining tartibi shu sababli bir-birining ustiga o'raladi, shuning uchun siz orqa o'qga chertish qilolmaysiz, tartibini tuzatdim

 <?xml version="1.0" encoding="utf-8"?>


    

    

        

        
    

2
qo'shib qo'ydi

Shunday qilib asboblar paneli Android-dagi nom satrini o'zgartirish uchun qo'shimcha moslashuvchanlikni beradi.

Nima uchun getActionBar ishlamayapti va getSupportActionBar dan foydalanishga majbur qilingansiz, chunki siz SupportLibrary dan foydalanishingiz kerak. SupportLibrary, oldingi SDK versiyalariga qaytarib muvofiqligi beradi.

Sarlavha satrini/nomini/aksiya panelini keng ravishda o'zgartirishni xohlasangiz   shundan so'ng asboblar panelini ishlating, aks holda harakat paneli ishlatiladi.

0
qo'shib qo'ydi

Dasturchi hujjatlari sifatida manifest faylida ota-onalik faoliyatini qo'shishning eng oddiy usuli bo'ladi.

 

va siz allaqachon qilgan java kodini, setSupportActionbar va setHomeAsUpEnabled.

Edited : its necessary to add up action for icon to be visible, as mentioned in Android Developer Docs

0
qo'shib qo'ydi
@EmmanuelGuther ma'lumot uchun rahmat, men to'g'ri javobni tahrir qildim
qo'shib qo'ydi muallif geniushkg, manba
Bu to'g'ri emas, hech bo'lmaganda CollapsingToolbarLayout dan foydalanayotgan bo'lsangiz. Android qo'shilishi: manifestdagi parentActivityName = "activityParentName" o'qni qaytarish tugmachasini ko'rsatmaydi, lekin agar siz getSupportActionBar() qo'shsangiz setDisplayHomeAsUpEnabled (rost); getSupportActionBar (). setDisplayShowHomeEnabled (rost); paydo bo'ladi
qo'shib qo'ydi muallif Emmanuel Guther, manba

Quyidagi kabi asboblar paneliga navigatsiya sekin urg'ichni qo'shing

toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onBackPressed();
        }
    });
0
qo'shib qo'ydi

Agar siz Save action yoki Share hisobi kabi ish satridan ba'zi harakatlarga murojaat qilmoqchi bo'lsangiz va siz onOptionsItemSelected usulini o'zgartirib qo'ysangiz, orqaga yoki home tugmasi bosilgan:

@Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case R.id.action_save:
        //save stuff
      break;

      //this is what you need to add to reference again back/home button
      case android.R.id.home:
        //do your stuff here, usually back to the home or close the current activity
        getActivity().finish();
      break;

      default:
      break;
    }
    return true;
0
qo'shib qo'ydi