Navigation Drawer



Download 363,66 Kb.
bet2/2
Sana16.06.2022
Hajmi363,66 Kb.
#677396
1   2

NavigationView


NavigationView yorlig'i ilova:headerLayout atributidagi o'z tartibiga havolani o'z ichiga oladi , bu nav_header_main.xml fayliga (pardaning yuqori qismiga) ishora qiladi, shuningdek, menyuga ishora qiluvchi ilova:menu atributidagi menyuni o'z ichiga oladi. resurs menyusi/activity_main_drawer.xml .
Nav_header_main.xml faylini ochamiz va pardaning belgilarini ko'rib chiqamiz.

xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="@dimen/nav_header_height"
android:background="@drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:theme="@style/ThemeOverlay.AppCompat.Dark">

android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
app:srcCompat="@mipmap/ic_launcher_round" />

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:text="Android Studio"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android.studio@android.com" />

Belgilash LinearLayout konteyneriga joylashtirilgan ImageView va ikkita TextViewdan iborat . Konteynerning foni drawable/side_nav_bar.xml resursida aniqlangan va gradient hisoblanadi.



android:shape="rectangle">
android:angle="135"
android:centerColor="#009688"
android:endColor="#00695C"
android:startColor="#4DB6AC"
android:type="linear" />

Qolgan atributlar o'z-o'zidan tushunarli va tushunarli.


Pardaning yuqori qismini XML orqali emas, balki dasturiy jihatdan sozlash mumkin (lekin shart emas).
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
View headerLayout = navigationView.inflateHeaderView(R.layout.nav_header_main);
ImageView headerImageView = headerLayout.findViewById(R.id.imageView);
Dizaynni qo'llab-quvvatlash kutubxonasining versiyalaridan birini yangilagandan so'ng , sarlavhaga endi boshqa kod orqali kirish mumkin.

View headerLayout = navigationView.getHeaderView(0);

Endi navigatsiya menyusi resursini ko'rib chiqing res/menu/activity_main_drawer.xml .





android:id="@+id/nav_camera"
android:icon="@drawable/ic_menu_camera"
android:title="Import" />
android:id="@+id/nav_gallery"
android:icon="@drawable/ic_menu_gallery"
android:title="Gallery" />
android:id="@+id/nav_slideshow"
android:icon="@drawable/ic_menu_slideshow"
android:title="Slideshow" />
android:id="@+id/nav_manage"
android:icon="@drawable/ic_menu_manage"
android:title="Tools" />




android:id="@+id/nav_share"
android:icon="@drawable/ic_menu_share"
android:title="Share" />
android:id="@+id/nav_send"
android:icon="@drawable/ic_menu_send"
android:title="Send" />


Menyu elementlarini yaratish printsipi standart bo'lib qoldi. Har bir menyu elementi belgi va matnga ega element tegidir. Guruh elementi guruhlash uchun ishlatiladi . Guruhdagi menyu elementlarining harakati android:checkableBehavior atributi tomonidan boshqariladi . Misol uchun yagona qiymatdan foydalaniladi - menyu bandini bosganingizda, u tanlangan bo'lib qoladi ( RadioButton kaliti printsipi ). Jami uchta variant mavjud.



  • bitta - siz guruhning bitta elementini tanlashingiz mumkin (radio tugmasi)

  • hammasi - guruhning barcha elementlarini tanlash mumkin (tasdiqlash qutisi)

  • hech biri - elementlar tanlanmagan

Android Support Design Library - ning 23-versiyasida barcha variant ishlamaydi va xuddi bitta kabi ishlaydi .
Shuni ham yodda tutingki, loyiha endi drawable-21 jildida joylashgan vektor chizmalariga havola qiladi .
Resurs fayli res/layout/app_bar_main.xml ga tegishli bo'lgan o'z ichiga tegni ko'rib chiqish qoladi . Biz Android qo'llab-quvvatlash dizayn kutubxonasi maqolasida o'rgangan Blank Activity naqshidan sizga tanish bo'ladi . Faqat u erda u activity_main.xml faylida edi , lekin bu erda u app_bar_main.xml fayliga ko'chirildi . Qolganlarning hammasi o'zgarishsiz qoldi. Biz takrorlamaymiz.
Endi parda bilan ishlash uchun faoliyat kodini o'rganamiz.
Activity sinfi OnNavigationItemSelectedListener interfeysini onNavigationItemSelected() usuli bilan amalga oshiradi :

public class MainActivity extends AppCompatActivity


implements NavigationView.OnNavigationItemSelectedListener {
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();

if (id == R.id.nav_camara) {


// Handle the camera action
} 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 drawer = (DrawerLayout) findViewById(R.id.drawer_layout);


drawer.closeDrawer(GravityCompat.START);
return true;
}
}

Kodning mantig'i oddiy menyu bilan ishlashdan farq qilmaydi. Tanlangan elementning identifikatori aniqlanadi va keyin siz o'z kodingizni yozishingiz kerak. Keyin drawerni yopish uchun closeDrawer() usuli chaqiriladi .


Menyuning birinchi elementi uchun kodni qo'shamiz.

if (id == R.id.nav_camera) {


// Handle the camera action
Toast.makeText(getApplicationContext(), "Вы выбрали камеру", Toast.LENGTH_SHORT).show();
}

"Orqaga" tugmasini bosganingizda, pardaning holati tekshiriladi. Agar parda ochiq bo'lsa ( isDrawerOpen() ), uni closeDrawer() usuli yordamida yoping .

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

onCreate () usulida drawer ishga tushiriladi.

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);


ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);


navigationView.setNavigationItemSelectedListener(this);
}

Endi loyihaga kiritilishi mumkin bo'lgan o'zgarishlar haqida gapiraylik.


O'ng tarafdagi pardani tortib olishni xohlaysizmi? layout_gravity atributining oxirgi qiymatini o'rnating . Odatda teskari harf tartibiga ega mamlakatlar uchun ishlatiladi.


...
android:layout_gravity="end"/>


Download 363,66 Kb.

Do'stlaringiz bilan baham:
1   2




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish