2016-03-22 3 views
0

Я пытаюсь установить действие с помощью 6 кнопок в макете из 2 столбцов на 3 строки. Кнопки должны быть одинаковыми по размеру, чтобы заполнить весь экран. Мне удалось получить его так, как мне нужно, но когда я вращаю телефон, это беспорядок. Проблема заключается в высоте LinearLayouts, которая жестко закодирована. Я попытался установить его в процентах в LinearLayouts, как и для кнопок, но он работает. это мой код.Установить вложенные группы просмотра в процентах

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
app:layout_behavior="@string/appbar_scrolling_view_behavior" 
tools:context="com.example.kenton.elderlyassistant.MainActivity" 
tools:showIn="@layout/activity_main" 
> 




<LinearLayout 
    android:id="@+id/up" 
    android:layout_width="match_parent" 
    android:layout_height="150dp" 
    android:orientation="horizontal" 
    > 

    <Button 

     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight=".50" 
     android:text="Send Message" 
     android:id="@+id/sendMessageButton" 
     android:layout_alignParentStart="true" 
     /> 

    <Button 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight=".50" 
     android:text="Select a contact Person" 
     android:id="@+id/findContactButton" 
     android:layout_alignParentStart="true" 
     /> 

</LinearLayout> 


<LinearLayout 
    android:id="@+id/mid" 
    android:layout_below="@+id/up" 
    android:layout_width="match_parent" 
    android:layout_height="150dp" 
    android:orientation="horizontal" 
    > 

    <Button 

     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight=".50" 
     android:text="Get GPS coordinates" 
     android:id="@+id/getGPSButton" 
     android:layout_below="@+id/up" 
     android:layout_alignParentStart="true" /> 
     /> 

    <Button 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight=".50" 
     android:text="Directions Home" 
     android:id="@+id/goHomeButton" 
     android:layout_below="@+id/getGPSButton" 
     android:layout_alignParentStart="true" /> 
     /> 

</LinearLayout> 

<LinearLayout 
    android:id="@+id/butt" 
    android:layout_below="@+id/mid" 
    android:layout_width="match_parent" 
    android:layout_height="150dp" 
    android:orientation="horizontal" 
    > 

    <Button 

     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight=".50" 
     android:text="Medication Reminders" 
     android:id="@+id/medicationReminderButton" 
     android:layout_alignParentStart="true" /> 
    /> 

    <Button 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight=".50" 
     android:text="Set your home address" 
     android:id="@+id/setAddressButton" 
     android:layout_alignParentStart="true" /> 
    /> 

</LinearLayout> 

+0

Вам не нужно 'Относительная Layout'. Просто используйте 'Linear Layout' и установите' layout_weight' соответственно. –

ответ

1

Используйте этот макет:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 


    <LinearLayout 
     android:id="@+id/up" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:orientation="horizontal"> 

     <Button 

      android:id="@+id/sendMessageButton" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:text="Send Message" /> 

     <Button 
      android:id="@+id/findContactButton" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:text="Select a contact Person" /> 

    </LinearLayout> 


    <LinearLayout 
     android:id="@+id/mid" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:orientation="horizontal"> 

     <Button 

      android:id="@+id/getGPSButton" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:text="Get GPS coordinates" /> 

     <Button 
      android:id="@+id/goHomeButton" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:text="Directions Home" /> 

    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/butt" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:orientation="horizontal"> 

     <Button 

      android:id="@+id/medicationReminderButton" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:text="Medication Reminders" /> 

     <Button 
      android:id="@+id/setAddressButton" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:text="Set your home address" /> 

    </LinearLayout> 
</LinearLayout> 
Смежные вопросы