简体   繁体   中英

Relative Layout child taking full height of screen

I am trying to get a chart and 3 buttons in an activity. The 3 buttons should align below the chart. I have the following XML but the buttons are just suppressed by the chart (EDIT for clarity: The buttons are not visible.)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".PlotActivity">

    <com.github.mikephil.charting.charts.LineChart
        android:id="@+id/chart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"/>

    <Button
        android:id="@+id/one_week_preset_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/chart"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:text="@string/one_week_label" />

    <Button
        android:id="@+id/one_month_preset_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/chart"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="@string/one_month_label" />

    <Button
        android:id="@+id/download_new_data_and_update_graph"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/chart"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="@string/download_and_update_label" />

</RelativeLayout>

I am pretty sure this can be done easily by two level nesting of linear layouts, but what is the problem with this XML and how to fix this. Any help appreciated.

The above XML produces the following screen as shown in android studio (note that chart occupies the whole screen and hence buttons are not visible)

图表占用完成屏幕

Use the chart vie as this. It is because you are telling the layout to align parent top and wrap_content which could be to the bottom of the parent . you should tell it that from bottom where is should be.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".PlotActivity">

<com.github.mikephil.charting.charts.LineChart
    android:id="@+id/chart"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_above="@+id/one_week_preset_button"
/>

<Button
    android:id="@+id/one_week_preset_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:text="@string/one_week_label" />

<Button
    android:id="@+id/one_month_preset_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:text="@string/one_month_label" />

<Button
    android:id="@+id/download_new_data_and_update_graph"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:text="@string/download_and_update_label" />

</RelativeLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM