2015-05-06 3 views
0

Я новый разработчик Android, и у меня вопрос о layout_gravity в линейном макете. Я бы хотел настроить две последние кнопки внизу экрана. Я пробовал вот так:android: layout_gravity = "bottom" not responsive

<?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="wrap_content" 
    android:orientation="vertical"> 

    <EditText 
     android:id="@+id/question" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:inputType="text" 
     android:text="@string/question" /> 

    <EditText 
     android:id="@+id/questione" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:inputType="text" 
     android:text="@string/questione" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/carne" /> 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/pesce" /> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/hotdog" /> 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/pizza" /> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/dolce" /> 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/vino" /> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom" 
     android:orientation="horizontal"> 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/logout" /> 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/avanti" /> 
    </LinearLayout> 
</LinearLayout> 

но эта команда не реагирует. Как я могу решить эту проблему?

ответ

0

Попробуйте ниже кода XML:
1. Как @Rajat Mehra спросит вас в комментарии, вам нужно сделать родительскую Linringayout Height «match_parent».
2. LinearLayout Заполните представление как по горизонтали, так и по вертикали. Таким образом, чтобы поместить LinearLayout внизу, вы задали вес другого LinearLayout или Put dummy view, чтобы заполнить пробел. В приведенном ниже коде я использовал компонент View для заполнения пустого пространства между двумя LinearLayout.

<?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"> 

    <EditText 
     android:id="@+id/question" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:inputType="text" 
     android:text="@string/question" /> 

    <EditText 
     android:id="@+id/questione" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:inputType="text" 
     android:text="@string/questione" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/carne" /> 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/pesce" /> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/hotdog" /> 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/pizza" /> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/dolce" /> 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/vino" /> 

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

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/logout" /> 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/avanti" /> 
    </LinearLayout> 
</LinearLayout> 

Вот как это выглядит:
enter image description here

0

Чтобы достичь этого, вы должны использовать RelativeLayout.

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

    <LinearLayout 
     android:layout_alignParentTop="true" 
     ...> 

     <!-- ... --> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_alignParentBottom="true" 
     ...> 

     <!-- ... --> 

    </LinearLayout> 

</RelativeLayout> 

Второй LinearLayout будет зафиксирован в нижней части экрана.

+0

@wisemann это не соответствует действительности. Я просто пропустил мой код: 'LinearLayout' имеет атрибут layout_alignParentBottom', если он находится внутри' RelativeLayout'. Исправлен мой код. – shkschneider

0

Вам нужно RelativeLayout, попробуйте этот макет

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

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <EditText 
      android:id="@+id/question" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:inputType="text" 
      android:text="@string/question"/> 

     <EditText 
      android:id="@+id/questione" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:inputType="text" 
      android:text="@string/questione"/> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 

      <Button 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:text="@string/carne"/> 

      <Button 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:text="@string/pesce"/> 

     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 

      <Button 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:text="@string/hotdog"/> 

      <Button 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:text="@string/pizza"/> 

     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 

      <Button 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:text="@string/dolce"/> 

      <Button 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:text="@string/vino"/> 

     </LinearLayout> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:layout_alignParentBottom="true"> 

     <Button 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/logout"/> 

     <Button 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/avanti"/> 
    </LinearLayout> 

</RelativeLayout> 
0

Вот простое решение, которое будет принимать минимальные действия для текущего макета:

  1. Ваш макет должен принять того, кто le screen, чтобы можно было разместить эти кнопки на дне. Установите android:layout_height="match_parent" в корень LinearLayout.
  2. Все элементы внутри этого LinearLayout возьмите некоторое пространство, убедитесь, что последний элемент, который является LinearLayout, обертывает кнопки, растягивается, чтобы занять все оставшееся пространство. Установите android:layout_weight="1" на внутренний LinearLayout.
  3. Теперь, когда внутренний LinearLayout растянут в нижней части экрана, вам нужно только установить android:gravity="bottom", чтобы переместить ваши кнопки на дно.

Предполагаю, что ваш макет работает для вас, но также рассмотрите возможность использования RelativeLayout, как и другие. Это может улучшить поведение вашего макета на разных экранах, однако вам придется перепроектировать весь макет, который не соответствует теме вашего исходного вопроса.

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