android - 下面带有 MPAndroidChart(或其他)的 ListView

标签 android android-layout android-listview mpandroidchart

Edit: I changed the title because isn't about MPAndroidChart, the same issue would happen with another view below the ListView.

我正在尝试为我的 Android 应用程序中的 Activity 设置布局。 此 Activity 由 2 个元素组成:ListView 和 MPAndroidChart。

我想要实现的是将 ListView 放置在顶部,采用它需要的高度 (wrap_content),并在其下方放置具有固定高度的图表。

我是android新手,所以我做的第一件事就是制作一个RelativeLayout,列表位于顶部,下面是图表。这在“纵向”中看起来不错(列表中几乎没有元素),但是当我在“横向”上尝试时,图表消失了......

然后我制作了一个 ScrollView 来包装整个内容(它是一个relativelayout),但我尝试了一些奇怪的行为(我猜是因为 ListView 的动态高度)。但无论如何,拥有两个不同的“卷轴”(一个用于列表,另一个用于整个 Activity )的想法似乎不太好。

好吧,我现在要做的是将高度分为两部分,并在顶部放置列表,在底部放置图表,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout  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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" >


    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:weightSum="2"
        android:orientation="vertical" >

        <ListView
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:id="@+id/list"
            android:listSelector="@android:color/transparent"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true">

        </ListView>

        <com.github.mikephil.charting.charts.LineChart
            android:id="@+id/chart"
            android:layout_width="match_parent"
            android:layout_below="@+id/list"
            android:layout_height="0dp"
            android:layout_weight="1"/>

    </LinearLayout>

</RelativeLayout>

但是我不太喜欢。

那么,对于如何做到这一点有什么想法吗?

最佳答案

试试这个代码

Portrait Mode

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#22bbcc"
    android:orientation="vertical">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_margin="4dp"
        android:layout_weight="0.50"
        android:background="#ccbb22" />

    <LinearLayout
        android:id="@+id/LinearLayout_chart"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_margin="4dp"
        android:layout_weight="0.50"
        android:background="#ddaaaa"
        android:orientation="horizontal">

        <!--You can have your chart here -->
    </LinearLayout>
</LinearLayout>

enter image description here

Landscape Mode

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="4dp"
    android:background="#22bbcc"
    android:orientation="horizontal">

    <ListView
        android:id="@+id/listView"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="4dp"
        android:layout_weight="0.50"
        android:background="#ccbb22" />

    <LinearLayout
        android:id="@+id/LinearLayout_chart"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="4dp"
        android:layout_weight="0.50"
        android:background="#ddaaaa"
        android:orientation="horizontal">

        <!--You can have your chart here -->
    </LinearLayout>

</LinearLayout>

enter image description here

如果可能,将PieChart动态添加到LinerLayout

private PieChart mChart;
private float[] yData = {5, 4, 5, 2}; 
private String[] xData = {"Item_one", "Item_two", "Item_Three","Item_Four"};
linearLayoutTwo = (LinearLayout)findViewById(R.id.LinearLayout_chart);
mChart = new PieChart(this);

    // configure pie chart
    mChart.setUsePercentValues(true);
    mChart.setDescription("");


    // enable hole and configure
    mChart.setDrawHoleEnabled(false);
    mChart.setHoleColorTransparent(true);
    mChart.setHoleRadius(0);
    mChart.setTransparentCircleRadius(0);

    // enable rotation of the chart by touch
    mChart.setRotationAngle(0);
    mChart.setRotationEnabled(false);

    ArrayList<Entry> yVals1 = new ArrayList<Entry>();

    for (int i = 0; i < yData.length; i++)
        yVals1.add(new Entry(yData[i], i));

    ArrayList<String> xVals = new ArrayList<String>();

    for (int i = 0; i < xData.length; i++)
        xVals.add(xData[i]);

    // create pie data set
    PieDataSet dataSet = new PieDataSet(yVals1, "");
    dataSet.setSliceSpace(0);
    dataSet.setSelectionShift(1);

    // add many colors
    ArrayList<Integer> colors = new ArrayList<Integer>();

    for (int c : new int[]{Color.rgb(124, 185, 232), Color.rgb(178, 132, 190), Color.rgb(201, 255, 229),
            Color.rgb(100, 148, 143)}) {
        colors.add(c);
    }


    colors.add(ColorTemplate.getHoloBlue());
    dataSet.setColors(colors);

    // instantiate pie data object now
    PieData data = new PieData(xVals, dataSet);
    data.setValueFormatter(new PercentFormatter());
    data.setValueTextSize(9f);
    data.setValueTextColor(Color.BLACK);

    mChart.setData(data);

    // undo all highlights
    mChart.highlightValues(null);

    // update pie chart
    mChart.invalidate();
    linearLayoutTwo.addView(mChart);
    // customize legends
    Legend l = mChart.getLegend();

    l.setPosition(Legend.LegendPosition.RIGHT_OF_CHART);
    l.setXEntrySpace(4);
    l.setYEntrySpace(4);

关于android - 下面带有 MPAndroidChart(或其他)的 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34528848/

相关文章:

android - 如何始终以编程方式设置默认选中的单选按钮android?

android:基本 Activity 上的按钮点击事件

android - 以编程方式滚动到屏幕末尾

ListView 内的 TextView 上的 Android setMovementMethod

android - ListView 的 getChildCount 方法不返回可见项目数的准确计数

android - Android 4.4 中不显示自定义字体

Android 兼容性 ExceptionInInitializerError

安卓通知错误

android - 在Listview中动态更改(List item)TextView的颜色

java - 在执行 swapCursor() 时如何处理 ConcurrentModificationException?