Android: مشاكل تخطيط XML
-
27-09-2019 - |
سؤال
لقد كنت أحاول إنشاء relativelayout مع رأس وجسم (قائمة) وتذييل. أريد أن يكون الرأس فوق كل شيء (محاذاة إلى أعلى) ، ويكون الجسم (القائمة) تحت الرأس (عند التمرير) وتكون التذييل (محاذاة إلى أسفل) فوق الجسم (القائمة) - فقط عندما يكون مرئيًا.
لقد تمكنت من القيام بذلك تمامًا عندما يتعلق الأمر بكيفية ظهور الأشياء ، ولكن يبدو أن تخطيط XML هو سبب المشكلات التي كنت أواجهها عند النقر على ListItems (انظر هنا, و هنا).
من أجل حل المشكلات ، اضطررت إلى لف ListView باستخدام LinearLayout ووضعها بين الرأس والتذييل (في XML). هذا يحل ListItems النقر فوق المشكلات ولكنه ينشئ مشكلة جديدة - يخفي تذييل عنصر ListView الأخير ، على الرغم من أن القائمة تنطلق تحت الرأس عند التمرير لأسفل.
أنا يائس إلى حد كبير هنا:
هذا هو XML الذي ينشئ التصميم المثالي ولكنه يتسبب في مشكلات النقر فوق عنصر القائمة:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#ffffff"
>
<RelativeLayout
android:background="@drawable/top_background"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:id="@+id/top_control_bar"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="Droidmarks"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/add_bookmark"
android:textSize="20dip"
android:textColor="#a0cb26"
android:shadowColor="#7a9b1b"
android:shadowRadius="5"
android:layout_marginLeft="5dip"
android:gravity="left|center" />
<Button
android:id="@+id/add_bookmark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:layout_alignParentRight="true"
android:text="Add"
/>
</RelativeLayout>
<LinearLayout
android:id="@+id/bottom_control_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@drawable/top_background"
android:visibility="invisible"
>
<Button
android:id="@+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:layout_alignParentRight="true"
android:text="Add"
/>
</LinearLayout>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/top_control_bar"
android:layout_above="@id/bottom_control_bar"
android:drawSelectorOnTop="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:cacheColorHint="#ffffff"
android:listSelector="@drawable/selected_item"
/>
<TextView android:id="@android:id/empty" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No Bookmarks found. You can add one by clicking the star."
android:layout_below="@id/top_control_bar" android:layout_above="@id/bottom_control_bar" />
</RelativeLayout>
هذا هو XML الذي يخفي عنصر القائمة الأخيرة ولكنه يحل مشكلات النقر:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#ffffff"
>
<RelativeLayout
android:background="@drawable/top_background"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:id="@+id/top_control_bar"
android:layout_alignParentTop="true"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="Droidmarks"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/add_bookmark"
android:textSize="20dip"
android:textColor="#a0cb26"
android:shadowColor="#7a9b1b"
android:shadowRadius="5"
android:layout_marginLeft="5dip"
android:gravity="left|center" />
<Button
android:id="@+id/add_bookmark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:layout_alignParentRight="true"
android:text="Add"
/>
</RelativeLayout>
<LinearLayout
android:id="@+id/listArea"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@id/top_control_bar"
>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
android:cacheColorHint="#ffffff" />
<TextView android:id="@android:id/empty" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No Bookmarks found. You can add one by clicking the star." />
</LinearLayout>
<LinearLayout
android:id="@+id/bottom_control_bar"
android:layout_above="@id/listArea"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@drawable/top_background"
android:orientation="horizontal"
android:visibility="visible"
>
<Button
android:id="@+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:layout_alignParentRight="true"
android:text="Add"
/>
</LinearLayout>
</RelativeLayout>
أود أن أقدر أي مساعدة لإيجاد حل بطريقة أو بأخرى لكلا القضيتين!
شكرًا لك!
المحلول
هل يمكنني أن أسأل لماذا تستخدم relativelayout على الإطلاق للحاوية؟ يبدو أنه مكان مثالي لاستخدام LinearLayout مع منطقة مركز مرجحة (ListView) تم إعدادها لأخذ جميع المساحة المتبقية بعد رأس وتذييل Wrap_height. تعتبر Relativelayouts رائعة لتبسيط التخطيطات المتداخلة بعمق والتي تحتوي على عناصر أفقية ورأسية ، ولكن طبقتك الخارجية هنا تحتوي فقط على 3 عناصر رأسية على أي حال ، لذا انتقل مع linearlayout مع orientation:vertical
ما لم أفتقد شيئًا هنا.
تحرير: باختصار ، جرب هذا ؛ يجب أن يمنحك هذا رأسًا ثابتًا ثابتًا ، وتذييلًا مرتفعًا ، ومنطقة الأوسط تشغل جميع المساحة المتبقية. تأكد من ضبط التصميم الخارجي على fill_parent
يتم ضبط الارتفاع والمنطقة الوسطى على ارتفاع ثابت ولكن مع أ layout_weight="1"
. أحب استخدام "0px" لـ Base Layout_height عندما أستخدم Layout_weight فقط لإبقائه واضحًا لي عندما أعود وأعيد قراءته لاحقًا.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#ffffff"
>
<RelativeLayout
android:background="@drawable/top_background"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:id="@+id/top_control_bar"
> <!-- snip - inner contents unchanged -->
</RelativeLayout>
<LinearLayout
android:id="@+id/listArea"
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="1"
android:orientation="horizontal"
><!-- snip - inner contents unchanged -->
</LinearLayout>
<LinearLayout
android:id="@+id/bottom_control_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/top_background"
android:orientation="horizontal"
android:visibility="visible"
><!-- snip - inner contents unchanged -->
</LinearLayout>
</LinearLayout>