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 .
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"/>
Do'stlaringiz bilan baham: |