Adding buttons and androidplot to a scrollview

2019-09-11 01:38发布

问题:

I can't seem to get buttons and androidplot together into a scrollview such that I can scroll down, past the androidplot to see the buttons. I am using androidplot 0.9.8.

Here is my xml file:

<?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>

EDIT: It seems however that when I specify a certain height for androidplot (e.g. android:layout_height="600dp" instead of android:layout_height="match_parent") it scrolls fine. Why is it that using match_parent prevents it from scrolling?

回答1:

Looks like you're using a value of match_parent for the height of your plot, which creates a chicken and egg scenario when used inside of a ScrollView as both views depend on the other to tell it what it's size is going to be.

The best thing to do here is to provide an explicit height for your plot in your layout xml. If that's not an option for you, adding:

android:fillViewport="true"

to your scrollview's XML may also do the trick. (Usually works but I've heard reports to the contrary under various circumstances) This blog post goes into a little more detail on the basic issue.



回答2:

I managed to hack it a little by programmatically setting a manual size to the plot. You can get the layout parameters of the plot and then manually change the height. I think there seems to be some sort of issue with height?

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);