2016-05-17 5 views
4

Я только что начал с Android и получил следующий код. Я хочу показать три кнопки в нижней части экрана, не переходя к относительной компоновке. Я попытался установить layout_weight и gravity внутренней линейной компоновки, но это приводит кнопки вниз, за ​​тремя кнопками меню телефона. Как я могу показать их над кнопками меню? Любая помощь будет оценена по достоинству.Как установить кнопки в нижней части экрана с линейным расположением

<?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/search_box" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="@string/search_box" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal" 
     android:paddingTop="80dp" 
     android:text="@string/welcome" 
     android:textSize="24sp" /> 

    <LinearLayout 
     style="?android:attr/buttonBarStyle" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:gravity="bottom" 
     android:orientation="horizontal"> 

     <Button 
      style="?android:attr/buttonBarButtonStyle" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/buttonB1" /> 

     <Button 
      style="?android:attr/buttonBarButtonStyle" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/buttonB2" /> 

     <Button 
      style="?android:attr/buttonBarButtonStyle" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/buttonB3" /> 
    </LinearLayout> 
</LinearLayout> 

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

Buttons I want to move to the bottom Buttons behind the menu buttons of phone

+0

Почему бы не использовать относительное расположение? Это сэкономит вам массу усилий и времени. –

+0

Вы пытались запустить это на устройстве? Интересно, выглядит ли это только экран предварительного просмотра, отображающий неверные результаты. –

+0

Ну, изображение, которое он разместил четко, - это не предварительный просмотр, а эмулятор. – Vucko

ответ

0

Попробуйте добавить маржу в нижней части каждого из кнопок. Добавьте это к каждой компоновке кнопок: android: layout_marginBottom = "20dp"

Если это не сработает, добавьте нижний предел к самому linearlayout.

+0

Он работает, но работает на эмуляторе, он добавляет дополнительное пространство под кнопками. Хотел иметь их на краю экрана. – Fleur

0

set TextView height = 0, weight = 1. И LinearLayout height = wrap content. Все будет в порядке. Пытаться !

0

Попробуйте

<?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/search_box" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="@string/search_box" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:background="#F00" 
     android:gravity="center_horizontal" 
     android:paddingTop="80dp" 
     android:text="@string/welcome" 
     android:textSize="24sp" /> 

    <LinearLayout 
     style="?android:attr/buttonBarStyle" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#FF0" 
     android:orientation="horizontal"> 

     <Button 
      style="?android:attr/buttonBarButtonStyle" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/buttonB1" /> 

     <Button 
      style="?android:attr/buttonBarButtonStyle" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/buttonB2" /> 

     <Button 
      style="?android:attr/buttonBarButtonStyle" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/buttonB3" /> 
    </LinearLayout> 
</LinearLayout> 
Смежные вопросы