android - what - كيفية محاذاة وجهات النظر في الجزء السفلي من الشاشة؟
xml layout code (12)
وهنا رمز تخطيطي.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:text="@string/welcome"
android:id="@+id/TextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>
<LinearLayout android:id="@+id/LinearLayout"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom">
<EditText android:id="@+id/EditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</EditText>
<Button android:text="@string/label_submit_button"
android:id="@+id/Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
</LinearLayout>
ما يبدو عليه هذا على اليسار وما أريد أن يبدو عليه هو على اليمين.
الإجابة الواضحة هي ضبط TextView على fill_parent على الارتفاع ولكن هذا لا يتسبب في ترك أي مساحة للزر أو حقل الإدخال. في الأساس ، المشكلة هي أنني أريد زر الإرسال وإدخال النص ليكون ارتفاعًا ثابتًا في الجزء السفلي وعرض النص لملء بقية المساحة ، وبالمثل في التخطيط الخطي الأفقي ، أريد أن يرسل الزر إرسال المحتوى لإدخال النص لملء بقية المساحة.
إذا تم إخبار العنصر الأول في Layear Layout بـ fill_parent ، فإنه يفعل ذلك تمامًا ، دون ترك أي مجال للعناصر الأخرى ، كيف يمكنني الحصول على عنصر يكون أولًا في تخطيط خطي لملء كل المساحة بعيدًا عن الحد الأدنى المطلوب لبقية العناصر العناصر في التخطيط؟
تصحيح:
كانت التخطيطات النسبية هي الحل في الواقع - شكرًا لك!
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:text="@string/welcome"
android:id="@+id/TextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
</TextView>
<RelativeLayout
android:id="@+id/InnerRelativeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<Button
android:text="@string/label_submit_button"
android:id="@+id/Button"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<EditText
android:id="@+id/EditText"
android:layout_width="fill_parent"
android:layout_toLeftOf="@id/Button"
android:layout_height="wrap_content">
</EditText>
</RelativeLayout>
</RelativeLayout>
1. استخدم ConstraintLayout
في تخطيط الجذر الخاص بك
وتعيين app:layout_constraintBottom_toBottomOf="parent"
للسماح للتخطيط في الجزء السفلي من الشاشة
<LinearLayout
android:id="@+id/LinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent">
</LinearLayout>
FrameLayout
في تخطيط الجذر الخاص بك
ما عليك android:layout_gravity="bottom"
تعيين android:layout_gravity="bottom"
في التنسيق
<LinearLayout
android:id="@+id/LinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="horizontal">
</LinearLayout>
LinearLayout
في تخطيط الجذر ( android:orientation="vertical"
)
(1) تعيين تخطيط android:layout_weight="1"
أعلى android:layout_weight="1"
<TextView
android:id="@+id/TextView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="welcome" />
(2) اضبط الطفل LinearLayout
على android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="bottom"
السمة الرئيسية هي ndroid:gravity="bottom"
، دع الطفل ndroid:gravity="bottom"
على الجزء السفلي من Layout.
<LinearLayout
android:id="@+id/LinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:orientation="horizontal">
</LinearLayout>
4. استخدم RelativeLayout
في تخطيط الجذر
وتعيين android:layout_alignParentBottom="true"
للسماح للتخطيط بأسفل الشاشة
<LinearLayout
android:id="@+id/LinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
</LinearLayout>
انتاج
أعتقد أنك يجب أن تجرب تخطيط نسبي .
إذا كان لديك مخطط نسبي يملأ الشاشة بأكملها ، فيجب عليك استخدام android:layout_alignParentBottom
لنقل الزر إلى أسفل الشاشة.
إذا لم يتم عرض وجهات نظرك في الجزء السفلي في تنسيق نسبي ، فربما يستلزم الشكل التوضيحي أعلاه كل المساحة. في هذه الحالة ، يمكنك وضع العرض الذي يجب أن يكون في الأسفل ، أولاً في ملف التخطيط الخاص بك ووضع بقية التخطيط فوق المشاهدات مع android:layout_above
. يؤدي ذلك إلى تمكين العرض السفلي من أخذ المساحة التي يحتاجها ، كما يمكن لبقية المخطط ملء كل ما تبقى من الشاشة.
إنشاء كل من HEADER و FOOTER ، هنا مثال
[تخطيط XML]
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/backgroundcolor"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="#FF0000">
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:background="#FFFF00">
</RelativeLayout>
</RelativeLayout>
[ لقطة شاشة ]
آمل أن يكون هذا مفيدا. شكر!!
استخدام الزر أدناه محاذاة رمز to bottem عملها
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn_back"
android:layout_width="100dp"
android:layout_height="80dp"
android:text="Back" />
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.97"
android:gravity="center"
android:text="Payment Page" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"/>
</LinearLayout>
</LinearLayout>
الجواب أعلاه (بواسطة Janusz) صحيح تمامًا ، ولكنني لا أشعر بشخصية 100٪ مع RelativeLayouts ، لذلك أفضل تقديم "حشو" ، فارغ TextView ، مثل هذا:
<!-- filler -->
<TextView android:layout_height="0dip"
android:layout_width="fill_parent"
android:layout_weight="1" />
قبل العنصر الذي يجب أن يكون في أسفل الشاشة.
في ScrollView
هذا لا يعمل ، لأن RelativeLayout
ScrollView
مع كل ما هو موجود في ScrollView
في أسفل الصفحة.
لقد قمت FrameLayout
باستخدام إطار FrameLayout
بشكل ديناميكي:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:fillViewport="true">
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">
<!-- content goes here -->
<!-- stretching frame layout, using layout_weight -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</FrameLayout>
<!-- content fixated to the bottom of the screen -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- your bottom content -->
</LinearLayout>
</LinearLayout>
</ScrollView>
لا تحتاج حتى إلى تضمين التنسيق relative
الثاني داخل المخطط الأول. ما عليك سوى استخدام android:layout_alignParentBottom="true"
في الزر و EditText .
لحالة مثل هذا ، استخدم دائمًا RelativeLayouts. لا يقصد LinearLayout لهذا الاستخدام.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/db1_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- Place your layout here -->
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:orientation="horizontal"
android:paddingLeft="20dp"
android:paddingRight="20dp" >
<Button
android:id="@+id/setup_macroSavebtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Save" />
<Button
android:id="@+id/setup_macroCancelbtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel" />
</LinearLayout>
</RelativeLayout>
متابعة الحل الأنيق لـ Timores ، لقد وجدت أن ما يلي يخلق تعبئة رأسية في LinearLayout رأسية وملء أفقي في خطي أفقي LinearLayout:
<Space
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
هذا يعمل أيضا.
<LinearLayout
android:id="@+id/linearLayout4"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_below="@+id/linearLayout3"
android:layout_centerHorizontal="true"
android:orientation="horizontal"
android:gravity="bottom"
android:layout_alignParentBottom="true"
android:layout_marginTop="20dp"
>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
/>
</LinearLayout>
يمكنك الاحتفاظ بالتخطيط الخطي الأولي من خلال تضمين التنسيق النسبي داخل التنسيق الخطي:
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:text="welcome"
android:id="@+id/TextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button android:text="submit"
android:id="@+id/Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true">
</Button>
<EditText android:id="@+id/EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/Button"
android:layout_alignParentBottom="true">
</EditText>
</RelativeLayout>
</LinearLayout>
يمكنك القيام بذلك باستخدام LinearLayout أو ScrollView أيضًا. في بعض الأحيان يكون من الأسهل تطبيق RelativeLayout. الشيء الوحيد الذي عليك فعله هو إضافة العرض التالي قبل طرق العرض التي تريد محاذاتها إلى أسفل الشاشة:
<View
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" />
يؤدي هذا إلى إنشاء عرض فارغ ، ملء الفراغ الفارغ ودفع المشاهدات التالية إلى أسفل الشاشة.