按百分比设置嵌套视图组

时间:2016-03-22 00:54:07

标签: android android-layout

我正在尝试在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>

1 个答案:

答案 0 :(得分: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>
相关问题