You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让两个androidplot.xy.XYPlot各占屏幕50%显示?

问题描述

尝试让两个androidplot.xy.XYPlot各占屏幕50%,通过android:weightSum实现:将每个XYPlot放入LinearLayout并设置权重为50,但实际运行时图表完全不显示。Android Studio设计视图显示符合预期,若给com.androidplot.xy.XYPlot设置固定高度(如250dp),图表可显示但无法实现各占屏幕50%的效果。

原XML代码如下:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="100"
    android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="50"
            android:background="#FF003B"
            android:orientation="vertical" >
                <com.androidplot.xy.XYPlot
                    android:id="@+id/pawGraph"
                    style="@style/APDefacto.Light"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:rangeTitle="Pressure (cmH2O)"
                    app:graphMarginBottom="0dp" />
        </LinearLayout>
        
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="50"
            android:background="#FFE600"
            android:orientation="vertical" >
                <com.androidplot.xy.XYPlot
                    android:id="@+id/tidalVolume"
                    style="@style/APDefacto.Light"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:graphMarginBottom="0dp"
                    app:lineLabelTextSizeBottom="0dp"
                    app:rangeTitle="Tidal Volume" />
        </LinearLayout>
</LinearLayout>
问题原因与修复方案

问题原因

XYPlot的layout_height设为wrap_content,但该控件没有内置的固有高度,当父LinearLayout通过layout_weight分配空间(自身高度为0dp)时,XYPlot无法计算出自己的显示尺寸,因此实际运行时不会渲染。

修复方法

修改每个XYPlot的布局参数,让它填满父LinearLayout的可用空间:

  • 将android:layout_width设为match_parent,确保图表宽度铺满父布局
  • 将android:layout_height设为match_parent,让图表高度占满父布局的全部空间

修改后的XML代码:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="100"
    android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="50"
            android:background="#FF003B"
            android:orientation="vertical" >
                <com.androidplot.xy.XYPlot
                    android:id="@+id/pawGraph"
                    style="@style/APDefacto.Light"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:rangeTitle="Pressure (cmH2O)"
                    app:graphMarginBottom="0dp" />
        </LinearLayout>
        
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="50"
            android:background="#FFE600"
            android:orientation="vertical" >
                <com.androidplot.xy.XYPlot
                    android:id="@+id/tidalVolume"
                    style="@style/APDefacto.Light"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:graphMarginBottom="0dp"
                    app:lineLabelTextSizeBottom="0dp"
                    app:rangeTitle="Tidal Volume" />
        </LinearLayout>
</LinearLayout>

额外优化(可选)

如果希望图表在父布局中居中显示,可给父LinearLayout添加android:gravity="center"属性,同时保持XYPlot的layout_height="match_parent"即可。

内容的提问来源于stack exchange,提问作者Tiến Nguyễn Minh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 10:02:06