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

الفصل 4 (الجزء 2): ConstraintLayout, CardView and Material Design

Precise positions, cards, and the rules that make a screen feel right

على طول: قواعد الـ ConstraintLayout، وبعدها الكارد والـ Material Design.

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

الفيديو قيد الإعداد

الفيديو قيد الإعداد. في الأثناء اقرأ المحاضرة المكتوبة وجرّب الأمثلة تحت.

رسوم متحركة

القيود بتمسك العنصر

أمثلة كود

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

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

المثال ١: توسيط بزر بقيدين

<Button    android:id="@+id/buttonSave"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="Save"    app:layout_constraintTop_toTopOf="parent"    app:layout_constraintBottom_toBottomOf="parent"    app:layout_constraintStart_toStartOf="parent"    app:layout_constraintEnd_toEndOf="parent" />

    

المثال ٢: عرض 0dp ياخد المساحة

<TextView    android:layout_width="0dp"    android:layout_height="wrap_content"    android:text="A long title that wraps"    app:layout_constraintStart_toStartOf="parent"    app:layout_constraintEnd_toEndOf="parent" />

    

المثال ٣: بطاقة منتج

<androidx.cardview.widget.CardView    android:layout_width="match_parent"    android:layout_height="wrap_content"    app:cardCornerRadius="12dp"    app:cardElevation="4dp">  <LinearLayout      android:layout_width="match_parent"      android:layout_height="wrap_content"      android:orientation="vertical"      android:padding="16dp">    <TextView android:text="Notebook" android:textSize="18sp" />    <TextView android:text="25 EGP" />    <Button android:text="Add" android:minHeight="48dp" />  </LinearLayout></androidx.cardview.widget.CardView>

    

اختبار قصير

اختبر فهمك

1. How many constraints does a view need at least?

2. layout_width="0dp" inside ConstraintLayout means:

3. A CardView holds:

4. The recommended spacing grid in Material Design is:

5. The smallest comfortable touch target is about:

تمارين

جرّب بإيدك

التمرين 1

وسّط TextView أفقياً ورأسياً بقيدين لكل اتجاه.

Centre a TextView horizontally and vertically with two constraints per axis.

Starter code:

<TextView    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="Centred" /><!-- TODO: add the four constraints -->

التمرين 2

حط بطاقة فيها عنوان وسعر وزرار، وخلي الزرار 48dp على الأقل.

Build a card with a title, a price and a button, with the button at least 48dp tall.

Starter code:

<androidx.cardview.widget.CardView    android:layout_width="match_parent"    android:layout_height="wrap_content">  <!-- TODO: title, price, button --></androidx.cardview.widget.CardView>

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

بطاقة درس في المقرر

الداتا: lesson-card: our own card with a lesson title, a duration and a button.

License / source: Our own layout; CardView reference by Google (link only). Reference page

الخطوات

  1. اعمل CardView بعرض الشاشة وارتفاع حسب المحتوى، وزوايا 12dp وظل 4dp.
  2. جواها LinearLayout رأسي بحشو 16dp.
  3. حط عنوان الدرس ومدته وزرار بمساحة 48dp على الأقل.
  4. اطبع سطر في الـ Log يقول إن البطاقة اتحمّلت.

Starter code:

setContentView(R.layout.activity_main);TextView title = findViewById(R.id.textTitle);Button open = findViewById(R.id.buttonOpen);// TODO: set the title text// TODO: log that the card is ready

مشروع نهاية الفصل

ملف المشروع العملي للفصل 4

ملف PDF فيه مشروع صغير بأسئلة عملية تطبّق بيها كل اللي اتعلمته في الفصل، مع حل نموذجي في الآخر.

افتح ملف المشروع (PDF)

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