انشاء navigation drawer وجعلها تدعم اللغة العربية android studio
03 March
من اصعب النوافد في الاندرويد هي هذه النافدة حيث تحتوي على كثير من الامور وصراحة اول تطبيق لي قمت انشإه كان يعتمد
على هذه النافدة و لنحمد الله انها تاتي مجهزة في الاندرويد ستوديو مما يوفر عليك الكثير من الوقت انشاء navigation drawer تدعم اللغة العربية android studio .
انصحك بالتجربة في مشروع جديد حتى تفهم كيفية عملها.
بكل بساطة نقوم بانشاء NDActivity بهذه الطريقة
اول ما ستلاحظه هو ظهور ثلاث ملفات مجلد layout و مجلد menu و ملف جافا
الان سنشرح الواحد تلو الاخر
اولا تقوم بحذف ملف content في مجلد layout
ملف activity_main :
يستحسن تركه كما هو
ملف app_bar_main :
هذا الملف الذي سيظهر فيه محتوى النافدة عند الظغط على اي زر في القائمة لدى سنقوم باضافة هذا الكود على هذا الشكل
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context="com.hachour.learnmore.french.main"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> <FrameLayout android:layout_marginTop="?attr/actionBarSize" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/fragmen"> </FrameLayout> </android.support.design.widget.CoordinatorLayout>
ملف nav_header_main :
يمكن التحكم في شكل الخلفية
اما ملف الموجود في menu
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <group android:checkableBehavior="single"> <item android:id="@+id/home" android:icon="@drawable/ic_home_black_24dp" android:title="@string/home" /> <item android:id="@+id/doros" android:icon="@drawable/ic_business_center_black_24dp" android:title="@string/doros" /> <item android:id="@+id/resume" android:icon="@drawable/ic_extension_black_24dp" android:title="@string/resume" /> <item android:id="@+id/eams" android:icon="@drawable/ic_description_black_24dp" android:title="@string/eams" /> <item android:id="@+id/correction" android:icon="@drawable/ic_description_black_24dp" android:title="@string/correction" /> <item android:id="@+id/produ" android:icon="@drawable/ic_view_carousel_black_24dp" android:title="@string/produ" /> </group> <item android:title="الدعوة"> <menu> <item android:id="@+id/share" android:icon="@drawable/ic_menu_share" android:title="@string/share" /> <item android:id="@+id/link_send" android:icon="@drawable/ic_menu_send" android:title="@string/send" /> </menu> </item> </menu>
فهو خاص بالقائمة التي تظهر لك على الجانب من ايقونات و واسماء ولك ان تغير كما تشاء فهو سهل و بسيط .
اما ملف الجافا فانقل اليه هذا الكود :
package com.hachour.learnmore.french; import android.content.Intent; import android.os.Bundle; import android.graphics.Typeface; import android.support.v4.app.FragmentTransaction; import android.support.v4.view.ViewCompat; import android.support.v4.view.GravityCompat; import android.support.design.widget.NavigationView; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.AppCompatActivity; import android.widget.TextView; import android.support.v7.widget.Toolbar; import android.view.MenuItem; import android.view.View; protected void onCreate(Bundle savedInstanceState) { public class main extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { @Override //الصفحة التي نريدها ان تظهر عند فتح النافدة super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); home ismail = new home(); fragmentTransaction.commit(); FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); fragmentTransaction.replace(R.id.fragmen,ismail); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); //الظهور من اليمين الى اليسار مهم لاصحاب التطبيقات الغةالعربية ViewCompat.setLayoutDirection(findViewById(R.id.nav_view), ViewCompat.LAYOUT_DIRECTION_RTL); //عند الضعط عليها تظهر الناف this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); setSupportActionBar(toolbar); //اتركها كما هي DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( //وضع التول بار على اليمين toggle.syncState(); ViewCompat.setLayoutDirection(findViewById(R.id.drawer_layout),ViewCompat.LAYOUT_DIRECTION_RTL); ViewCompat.setLayoutDirection(findViewById(R.id.toolbar),ViewCompat.LAYOUT_DIRECTION_RTL); drawer.setDrawerListener(toggle); //استدعاءالناف و ربطها بحدت و هو NavigationItemSelectedListener if (drawer.isDrawerOpen(GravityCompat.START)) { NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this); } @Override public void onBackPressed() { DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); } else { super.onBackPressed(); getSupportActionBar().setTitle("الرئيسية"); } } @SuppressWarnings("StatementWithEmptyBody") //عند الظعط على العنصر في الناف @Override public boolean onNavigationItemSelected(MenuItem item) { // عند الضغط على العنصر يقوم بمهمة //id يمثل الزر المضغوط عليه int id = item.getItemId(); //عند الظغط على الرئيسية if (id == R.id.home) { //وضع عنوان الصفحة } else if (id == R.id.doros) { //وضع عنوان الصفحة getSupportActionBar().setTitle("الروايات"); } else if (id == R.id.resume) { //وضع عنوان الصفحة getSupportActionBar().setTitle("ملخصات"); }else if (id == R.id.eams) { //وضع عنوان الصفحة getSupportActionBar().setTitle("نمادج امتحانات"); }else if (id == R.id.correction) { DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); } else if (id == R.id.produ) { //وضع عنوان الصفحة getSupportActionBar().setTitle("نمادج production"); } else if (id == R.id.share) { //زر المشاركة و نقل المعلومات المراد نقلها getSupportActionBar().setTitle("مشاركة"); } else if (id == R.id.link_send) { } else if (id == R.id.suppor) { } drawer.closeDrawer(GravityCompat.START); return true; } }
بعد الانتهاء من تجهيز NDA
تقوم بانشاء fragement تضع فيه اي شئ تريده مثالا هذا النص " هذه هي النافذة الاولى " الى ملف الجافا و لا بد انك لاحظت ان الشروط تركتها فارغة الان قم بنسخ هذا الكود داخل كل شرط الى اخره
oeuvre fragment = new oeuvre(); FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); //لاحظ ان الفراكمنت يوضع داخل الفريم لايوت التي اضفتها في ملفapp_bar_main fragmentTransaction.replace(R.id.bella,fragment); fragmentTransaction.commit();
fragement يمكنك اضافة ما يكفيك من على حسب القائمة .
وهنيئا لك .
facebook لا تنسى مرسلتي على ان وجدت اي مشكل.
اترك لنا تعليقا