كل المقررات

الرئيسية / مقرر تطبيقات الموبايل / الفصل 4 الجزء 1

الفصل 4 (الجزء 1): Layouts and view groups

Views, view groups, and the layout types Android gives you

على طول: الشاشة شجرة عناصر، والـ layout هو اللي بيرتبهم.

فيديو المحاضرة

فيديو المحاضرة

بعد الفيديو: الأمثلة والاختبار والتمارين والمشروع الصغير تحت.

رسوم متحركة

أنواع الـ Layout

أمثلة كود

أمثلة قصيرة وبسيطة

كل مثال وراه زرار «شغّل خطوة بخطوة»: بيعلّم على السطر اللي بيتنفّذ ويطبع الناتج زي ما بيتنفّذ بالظبط. ده شرح تنفيذ الكود سطر بسطر، مش مترجم حقيقي جوه الصفحة.

المثال ١: 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>

التمرين 2

حط الجدول جوه ScrollView عشان يشتغل على الشاشات الصغيرة.

Wrap the table in a ScrollView so it works on small screens.

Starter code:

<!-- TODO: ScrollView around the TableLayout -->

مشروع صغير في آخر المحاضرة

شاشة بروفايل بـ 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

الخطوات

  1. اعمل LinearLayout رأسي بعرض وارتفاع ياخدوا الشاشة.
  2. حط عنوان فوق، ونص في الوسط بـ weight 1، وسطر في الآخر.
  3. في onCreate اطبع سطر يقول إن الثلاثة اتحمّلوا.
  4. شغّل التطبيق: العنوان فوق والسطر تحت والنص بيملا اللي بينهم.

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

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).