|
android:background="?attr/colorPrimary"
|
bet | 3/3 | Sana | 25.05.2024 | Hajmi | 1,65 Mb. | | #253821 |
Bog'liq M10 (4) android:background="?attr/colorPrimary" app:title="App menu" app:layout_constraintTop_toTopOf="parent" android:minHeight="?attr/actionBarSize" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> MainActivity.java package com.example.menuapp; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import androidx.appcompat.widget.Toolbar; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main_menu, menu); return true; } } Menyuni yaratish getMenuInflater metodi MenuInflater obyektini qaytaradi. inflate metod birinchi parametr sifatida xml formatidagi deklarativ menyu tavsifimizni ifodalovchi resursni oladi va ikkinchi parametr sifatida uzatilgan menyu obyektini u bilan to'ldiradi. Dasturni ishga tushiramiz va yuqori o'ng burchakda joylashgan menyuni ko'rishimiz mumkin. onOptionsItemSelected metodi @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) { int id = item.getItemId(); if (id == R.id.about_app){ Toast.makeText(this, "Ilova haqidagi elementni tanladingiz", Toast.LENGTH_SHORT).show(); } return super.onOptionsItemSelected(item); }
Menyu bandlaridan birini bossak, hech narsa bo'lmaydi. Harakatlarni menyuga ulash uchun faoliyat klassida onOptionsItemSelected metodini bog’lash kerak bo’ladi.
|
| |