2016-03-22 9 views
0

2列×3行のレイアウトで6つのボタンを使用してアクティビティを設定しようとしています。 ボタンは画面全体を埋めるためにサイズが同じでなければなりません。 私は必要に応じてそれを手に入れることができましたが、私が電話を回しているときには、ものを混乱させます。 問題は、ハードコードされた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'sを使って' 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> 
関連する問題