فيديو المحاضرة
الفيديو قيد الإعداد
الفيديو قيد الإعداد. في الأثناء اقرأ المحاضرة المكتوبة وجرّب الأمثلة تحت.
رسوم متحركة
أمثلة كود
أمثلة قصيرة وبسيطة
كل مثال وراه زرار «شغّل خطوة بخطوة»: بيعلّم على السطر اللي بيتنفّذ ويطبع الناتج زي ما بيتنفّذ بالظبط. ده شرح تنفيذ الكود سطر بسطر، مش مترجم حقيقي جوه الصفحة.
المثال ١: LinearLayout رأسي
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Title" /> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:text="Body" /></LinearLayout>
المثال ٢: RelativeLayout
<RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:text="Profile" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/title" android:layout_alignParentEnd="true" android:text="Edit" /></RelativeLayout>
المثال ٣: TableLayout
<TableLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:stretchColumns="1"> <TableRow> <TextView android:text="Name" /> <TextView android:text="Score" /> </TableRow> <TableRow> <TextView android:text="Mona" /> <TextView android:text="18" /> </TableRow></TableLayout>
اختبار قصير
اختبر فهمك
1. A ViewGroup is:
2. Which layout puts children at exact x and y and breaks on other screen sizes?
3. layout_weight does what?
4. Which layout is the current recommendation for a whole screen?
5. A ScrollView can contain:
تمارين
جرّب بإيدك
التمرين 1
اعمل LinearLayout رأسي فيه عنوان ونص، والنص ياخد باقي الشاشة بـ weight.
Make a vertical LinearLayout with a title and a body, where the body takes the rest with weight.
Starter code:
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- TODO: title TextView --> <!-- TODO: body TextView with weight --></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content" android:text="Title" />
<TextView android:layout_width="match_parent"
android:layout_height="0dp" android:layout_weight="1" android:text="Body" />
</LinearLayout>
Expected output
system: body fills the remaining height
التمرين 2
حط الجدول جوه ScrollView عشان يشتغل على الشاشات الصغيرة.
Wrap the table in a ScrollView so it works on small screens.
Starter code:
<!-- TODO: ScrollView around the TableLayout -->
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>
Expected output
system: the table scrolls when it is taller than the screen
مشروع صغير في آخر المحاضرة
شاشة بروفايل بـ LinearLayout
الداتا: profile: our own layout with a header, a body and a footer.
License / source: Our own layout; LinearLayout guide by Google (link only). Reference page
الخطوات
- اعمل LinearLayout رأسي بعرض وارتفاع ياخدوا الشاشة.
- حط عنوان فوق، ونص في الوسط بـ weight 1، وسطر في الآخر.
- في onCreate اطبع سطر يقول إن الثلاثة اتحمّلوا.
- شغّل التطبيق: العنوان فوق والسطر تحت والنص بيملا اللي بينهم.
Starter code:
setContentView(R.layout.activity_main);TextView header = findViewById(R.id.textHeader);TextView body = findViewById(R.id.textBody);TextView footer = findViewById(R.id.textFooter);// TODO: log that three parts are on the screen
setContentView(R.layout.activity_main);
TextView header = findViewById(R.id.textHeader);
TextView body = findViewById(R.id.textBody);
TextView footer = findViewById(R.id.textFooter);
Log.d("Profile", "header, body and footer loaded");
Expected output
D/Profile: header, body and footer loaded
Credits: the topic order follows the course notes "Mobile Application Development 1" by Dr. Muhammad Kashif Saeed (Applied College, Muhayil, King Khalid University), shared by the course owner. The explanations, diagrams, data and code in this lecture are our own; we copy nothing from the notes. Extra reference: Android developer documentation: layout types, ConstraintLayout, CardView, and Material Design (developer.android.com, m3.material.io).