将按钮和 androidplot 添加到滚动视图

Adding buttons and androidplot to a scrollview

我似乎无法将按钮和 androidplot 一起放入滚动视图中,这样我就可以向下滚动,越过 androidplot 来查看按钮。我正在使用 androidplot 0.9.8.

这是我的 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:ap="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="example.example.MainActivity">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/plotLayout">
            <com.androidplot.xy.XYPlot
                    android:id="@+id/plot"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    ap:Label="test"
                    ap:domainLabel="test"
                    ap:rangeLabel="test"
                    ap:renderMode="use_main_thread"/>
            <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:layout_below="@id/plot">
                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:text="test"/>
                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:text="test"/>
                    </LinearLayout>
        </RelativeLayout>
</ScrollView>

编辑:然而,当我为 androidplot 指定特定高度时(例如 android:layout_height="600dp" 而不是 android:layout_height="match_parent"),它似乎可以正常滚动。为什么使用 match_parent 会阻止它滚动?

我设法通过以编程方式为情节设置手动大小来破解它。您可以获取绘图的布局参数,然后手动更改高度。我觉得身高好像有点问题?

XYPlot plot = (XYPlot) findViewById(R.id.plot);
LayoutParams lp = plot.getLayoutParams();
lp.height = 1710; // You can set it to whatever height you require it to be, 1710 is just an example
plot.setLayoutParams(lp);

看起来您正在使用值 match_parent 作为地块的高度,这在 ScrollView 内部使用时会产生先有鸡还是先有蛋的场景,因为这两个视图相互依赖告诉它它的大小。

此处最好的做法是在布局中为绘图提供明确的高度 xml。如果这不是您的选择,请添加:

android:fillViewport="true"

滚动视图的 XML 也可以解决问题。 (通常有效,但我在各种情况下听到了相反的报告)This blog post 对基本问题进行了更详细的介绍。