Проблема с макетом Android

#android #layout

#Android #макет

Вопрос:

Я пытаюсь создать простой макет, но не могу найти более удобный способ сделать это. Макет должен содержать 3 элемента: TextView сверху, EditText (с scrollview) посередине и button внизу. Я создал этот код:

     <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:weightSum="1"
  android:orientation="vertical"
  android:layout_height="fill_parent"
  android:layout_width="fill_parent">

  <LinearLayout android:layout_weight="0.97"
  android:layout_height="wrap_content"
  android:layout_width="fill_parent"
  android:id="@ id/linearLayout18"
  android:orientation="vertical">
    <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"         
        android:text="Text:"
        android:gravity="center_vertical"/>

    <ScrollView android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <EditText 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:id="@ id/txtMessage"
        android:text=""/>

    </ScrollView>
    </LinearLayout>
    <LinearLayout
    android:layout_weight="0.03"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:id="@ id/linearLayout1"
    android:orientation="vertical">
        <Button 
        android:text="Next"
        android:id="@ id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />       
    </LinearLayout>
</LinearLayout>
  

Но это дает мне макет, подобный этому: МакетКак вы можете видеть, проблема в том, что EditText не будет занимать все доступное пространство по вертикали (вплоть до кнопки). Как это решить?

Ответ №1:

Просто используйте android:fillViewport="true" в своем ScrollView

Комментарии:

1. Два одинаковых (и правильных) ответа. Я выбрал ваш — вы ответили первым 🙂 Простое и эффективное решение, спасибо! 🙂

Ответ №2:

попробуйте relativelayout вместо linear вот тот же код

     <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_height="fill_parent"
  android:layout_width="fill_parent">

  <RelativeLayout android:layout_height="wrap_content"
  android:layout_width="fill_parent"
  android:id="@ id/linearLayout18" android:layout_alignParentTop="true">
    <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"         
        android:text="Text:"
         android:id="@ id/textMessage"
        android:gravity="center_vertical" android:layout_alignParentTop="true"/>

    <ScrollView android:layout_width="fill_parent"
    android:layout_below="@ id/textMessage" android:layout_above="@ id/button1" android:scrollbars="horizontal|vertical" android:layout_height="fill_parent" android:fillViewport="true">

    <EditText 
        android:id="@ id/txtMessage"
        android:text="" android:layout_height="fill_parent" android:layout_width="fill_parent"/>

    </ScrollView>


        <Button 
        android:text="Next"
        android:id="@ id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"/>       
    </RelativeLayout>
</RelativeLayout>
  

Комментарии:

1. Что ж, добавление просто android:FillViewport=»true» решило проблему, почему я должен использовать «относительный макет»?

Ответ №3:

Попробуйте это….Это идеально…Я протестировал это в своей Android Studio…..Это тот же макет, что и вы хотите….

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent">

    <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Text:"
            android:id="@ id/textview1"
            android:layout_alignParentTop="true"/>


        <ScrollView android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:fillViewport="true"
            android:layout_above="@id/btn"
            android:layout_below="@id/textview1">

            <EditText
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:id="@ id/txtMessage"
                android:text=""/>

        </ScrollView>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/txtMessage"
        android:id="@ id/btn"
        android:text="Next"
        android:layout_alignParentBottom="true"/>

    </RelativeLayout>