2016-03-22 22 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'。只需使用“线性布局”并相应地设置“布局权重”即可。 –

回答

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>