• Toolbar toolbar = findViewById(R.id.toolbar);
  • } Menyuni yaratish
  • Dasturni ishga tushiramiz va yuqori ong burchakda joylashgan menyuni korishimiz mumkin.
  • android:background="?attr/colorPrimary"




    Download 1,65 Mb.
    bet3/3
    Sana25.05.2024
    Hajmi1,65 Mb.
    #253821
    1   2   3
    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.
    Download 1,65 Mb.
    1   2   3




    Download 1,65 Mb.

    Bosh sahifa
    Aloqalar

        Bosh sahifa



    android:background="?attr/colorPrimary"

    Download 1,65 Mb.