Re-using Layouts - اعاده الاستخدام
لاعادة استخدام layout بكفائة يمكن استخدام <include/> و<merge/> لتضمين layout داخل layout الحالية
اعاده الاستخدام قوي جدا لانه يسمح لك بتصمين layout معقده بدلا من اعادة اضافتها في كل الواجهات
مثال : نريد تضمين هذه layout
ملاحظة: tools:showIn تستخدم وقت التصميم فقط لاضهار هذه layout في واجهات اخرى لغرض التعديل
اعاده الاستخدام قوي جدا لانه يسمح لك بتصمين layout معقده بدلا من اعادة اضافتها في كل الواجهات
مثال : نريد تضمين هذه layout
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/titlebar_bg" tools:showIn="@layout/activity_main" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/gafricalogo" /> </FrameLayout>
ملاحظة: tools:showIn تستخدم وقت التصميم فقط لاضهار هذه layout في واجهات اخرى لغرض التعديل
استخدم include
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/app_bg" android:gravity="center_horizontal"> <include layout="@layout/titlebar"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/hello" android:padding="10dp" /> ... </LinearLayout>
و يمكن التعديل على خواص الlayout من خلال include مثلا :
<include android:id="@+id/news_title" android:layout_width="match_parent" android:layout_height="match_parent" layout="@layout/title"/>
استخدام <merge/>
يزيل ال view groups من التسلسل الهرمي لل layout عند تضمينها في layout اخرى
مثال :
اذا كان لدينا layout تحتوي على root وايضا view ونحن لا نريد تضمين root الى layout لانه يوثر على الاداء ويزداد عمق layout نستخدم marge لهذة الغرض
<merge xmlns:android="http://schemas.android.com/apk/res/android"> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/add"/> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/delete"/> </merge>
عند التضمين نستخدم include
تعليقات
إرسال تعليق