0

Я хочу несколько EditTexts с кнопками справа. Прямо сейчас у меня только что Edittexts заполняет родителя трюком 0dp. Если я добавлю кнопку, высота EditText будет уменьшаться с помощью wrap_content. Если я делаю match_parent, один EditText заполняет весь экран.Horizonal LinearLayouts в вертикальном LinearLayout

RelativeLayout не работает, потому что вы не можете заставить его соответствовать родительскому объекту так же, как LinearLayout.

Я думал о том, чтобы получить высоту экрана, а затем установил высоту макета на 1/7 (поскольку у меня есть 7 EditTexts). Правдоподобно?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 
    <EditText android:id="@+id/box_0" 
     android:layout_weight="1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:hint="@string/edit_task" 
     android:saveEnabled="true" 
     android:inputType="textCapSentences" 
     android:maxLength="32" 
     android:padding="10dp"/> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/button_send" 
     android:layout_gravity="end"/> 
    <!--red--> 
    </LinearLayout> 
<EditText android:id="@+id/box_1" 
    android:layout_weight="1" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:saveEnabled="true" 
    android:inputType="textCapSentences" 
    android:maxLength="32" 
    android:padding="10dp"/> 
<EditText android:id="@+id/box_2" 
    android:layout_weight="1" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:saveEnabled="true" 
    android:inputType="textCapSentences" 
    android:maxLength="32" 
    android:padding="10dp"/> 
<EditText android:id="@+id/box_3" 
    android:layout_weight="1" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:saveEnabled="true" 
    android:inputType="textCapSentences" 
    android:maxLength="32" 
    android:padding="10dp"/> 
<EditText android:id="@+id/box_4" 
    android:layout_weight="1" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:saveEnabled="true" 
    android:inputType="textCapSentences" 
    android:maxLength="32" 
    android:padding="10dp"/> 
<EditText android:id="@+id/box_5" 
    android:layout_weight="1" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:saveEnabled="true" 
    android:inputType="textCapSentences" 
    android:maxLength="32" 
    android:padding="10dp"/> 
<EditText android:id="@+id/box_6" 
    android:layout_weight="1" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:saveEnabled="true" 
    android:inputType="textCapSentences" 
    android:maxLength="32" 
    android:padding="10dp"/> 
</LinearLayout> 

Я только сделал двойной LinearLayout в первом EditText.

http://i.imgur.com/t4hN6nO.jpg

Посмотрите, как второй больше, чем остальные, чтобы компенсировать и первый меньше?

+0

Какой тип макета вы хотите? –

ответ

0

Почему бы вам не добавить ScrollView? Будет множество устройств, которые никогда не будут правильно соответствовать 7 editText на экране.

В любом случае, не рекомендуется использовать вложенные LinearLayout с весом. Но вы можете добиться того, что хотите сделать таким образом.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:orientation="horizontal"> 

    <EditText 
     android:id="@+id/box_0" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:hint="@string/edit_task" 
     android:inputType="textCapSentences" 
     android:maxLength="32" 
     android:padding="10dp" 
     android:saveEnabled="true" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="end" 
     android:text="@string/button_send" /> 
    <!--red--> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:orientation="horizontal"> 

    <EditText 
     android:id="@+id/box_1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:hint="@string/edit_task" 
     android:inputType="textCapSentences" 
     android:maxLength="32" 
     android:padding="10dp" 
     android:saveEnabled="true" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="end" 
     android:text="@string/button_send" /> 
    <!--red--> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:orientation="horizontal"> 

    <EditText 
     android:id="@+id/box_2" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:hint="@string/edit_task" 
     android:inputType="textCapSentences" 
     android:maxLength="32" 
     android:padding="10dp" 
     android:saveEnabled="true" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="end" 
     android:text="@string/button_send" /> 
    <!--red--> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:orientation="horizontal"> 

    <EditText 
     android:id="@+id/box_3" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:hint="@string/edit_task" 
     android:inputType="textCapSentences" 
     android:maxLength="32" 
     android:padding="10dp" 
     android:saveEnabled="true" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="end" 
     android:text="@string/button_send" /> 
    <!--red--> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:orientation="horizontal"> 

    <EditText 
     android:id="@+id/box_4" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:hint="@string/edit_task" 
     android:inputType="textCapSentences" 
     android:maxLength="32" 
     android:padding="10dp" 
     android:saveEnabled="true" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="end" 
     android:text="@string/button_send" /> 
    <!--red--> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:orientation="horizontal"> 

    <EditText 
     android:id="@+id/box_5" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:hint="@string/edit_task" 
     android:inputType="textCapSentences" 
     android:maxLength="32" 
     android:padding="10dp" 
     android:saveEnabled="true" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="end" 
     android:text="@string/button_send" /> 
    <!--red--> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:orientation="horizontal"> 

    <EditText 
     android:id="@+id/box_6" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:hint="@string/edit_task" 
     android:inputType="textCapSentences" 
     android:maxLength="32" 
     android:padding="10dp" 
     android:saveEnabled="true" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="end" 
     android:text="@string/button_send" /> 
    <!--red--> 
</LinearLayout> 

Лучшее решение, которое требует немного больше работы, как вы сказали, вычисления высоты с ScreenHeight/7

+0

Спасибо! Позже я выясню расчетную часть для лучшей производительности. –

0

Есть две основные причины:
1. Установить android:layout_weight="1" в Linringayout, который содержит EditText и кнопку.
2. Установите android:layout_height="match_parent" в этом EditText, чтобы он был таким же высоким, как и его родитель.

И тогда код раскладка будет что-то вроде этого:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:orientation="horizontal"> 

    <EditText 
     android:id="@+id/box_0" 
     android:layout_weight="1" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:saveEnabled="true" 
     android:inputType="textCapSentences" 
     android:hint="hello" 
     android:maxLength="32" 
     android:padding="10dp" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="test" 
     android:layout_gravity="end|center_vertical" /> 
    <!--red--> 
</LinearLayout> 

<EditText 
    android:id="@+id/box_1" 
    android:layout_weight="1" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:saveEnabled="true" 
    android:inputType="textCapSentences" 
    android:maxLength="32" 
    android:padding="10dp" /> 

<EditText 
    android:id="@+id/box_2" 
    android:layout_weight="1" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:saveEnabled="true" 
    android:inputType="textCapSentences" 
    android:maxLength="32" 
    android:padding="10dp" /> 

<EditText 
    android:id="@+id/box_3" 
    android:layout_weight="1" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:saveEnabled="true" 
    android:inputType="textCapSentences" 
    android:maxLength="32" 
    android:padding="10dp" /> 

<EditText 
    android:id="@+id/box_4" 
    android:layout_weight="1" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:saveEnabled="true" 
    android:inputType="textCapSentences" 
    android:maxLength="32" 
    android:padding="10dp" /> 

<EditText 
    android:id="@+id/box_5" 
    android:layout_weight="1" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:saveEnabled="true" 
    android:inputType="textCapSentences" 
    android:maxLength="32" 
    android:padding="10dp" /> 

<EditText 
    android:id="@+id/box_6" 
    android:layout_weight="1" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:saveEnabled="true" 
    android:inputType="textCapSentences" 
    android:maxLength="32" 
    android:padding="10dp" /> 

И предварительный просмотр изображения:
Надеюсь, что это помогает!

0

Попробуйте это:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:orientation="horizontal"> 
    <EditText android:id="@+id/box_0" 
     android:layout_weight="1" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:hint="@string/edit_task" 
     android:saveEnabled="true" 
     android:inputType="textCapSentences" 
     android:maxLength="32" 
     android:padding="10dp"/> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:text="@string/button_send" 
     android:layout_gravity="end"/> 
    <!--red--> 
    </LinearLayout> 

Вы должны сделать свой horizontal Linear Layout, матч с другими Edit text, поэтому я изменил layout_height к 0dp, и я также добавить layout weight из 1. Это, предполагают, чтобы быть он то же самое к вашему другому Edit text s. Также я изменил layout_height в Edit Text на "match_parent". Остальная часть кода должна оставаться прежней.

Смежные вопросы