列表视图与MPAndroidChart(或其他)以下 [英] Listview with a MPAndroidChart(or whatever) below

查看:273
本文介绍了列表视图与MPAndroidChart(或其他)以下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

编辑:我改了称呼,因为是不是MPAndroidChart,同
  问题会发生与下面的ListView另一种观点。


我想在我的Andr​​oid应用程序的活动设置的布局。
本次活动由2元,一个ListView和MPAndroidChart。

我想实现的是将在顶部的ListView,同时它需要( WRAP_CONTENT )和它下面的图表与固定高度的高度。

我在Android新手,所以我做的第一件事是做的RelativeLayout与列表的顶部和低于,图表。这似乎在肖像精(名单有几个元素),但是当我试图在景观图表消失...

于是我做了一个滚动型包装的全部内容(这是一个RelativeLayout的),但我尝试了一些奇怪的行为(我猜是因为列表视图的动态高度)。但不管怎么说,有两个不同的卷轴的观念(一个用于列表,另一个用于整个活动)似乎并不很漂亮。

那么,我现在做的是分两种的高度和对顶部,列表,并在底部,图表,就像这样:

 <?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:paddingLeft =@扪/ activity_horizo​​ntal_margin
    机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
    机器人:paddingTop =@扪/ activity_vertical_margin
    机器人:paddingBottom会=@扪/ activity_vertical_margin
    应用:layout_behavior =@字符串/ appbar_scrolling_view_behavior>
    < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT
        机器人:weightSum =2
        机器人:方向=垂直>        < ListView控件
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =0dp
            机器人:layout_weight =1
            机器人:ID =@ + ID /列表
            机器人:listSelector =@机器人:彩色/透明
            机器人:layout_alignParentTop =真
            机器人:layout_alignParentLeft =真
            机器人:layout_alignParentStart =真正的>        < /&的ListView GT;        < com.github.mikephil.charting.charts.LineChart
            机器人:ID =@ + ID /图
            机器人:layout_width =match_parent
            机器人:layout_below =@ + ID /列表
            机器人:layout_height =0dp
            机器人:layout_weight =1/>    < / LinearLayout中>< / RelativeLayout的>

但我真的不喜欢它。

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


解决方案

试试这个code


  

人像模式


 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:背景=#22bbcc
    机器人:方向=垂直>    < ListView控件
        机器人:ID =@ + ID /的ListView
        机器人:layout_width =match_parent
        机器人:layout_height =0dp
        机器人:layout_margin =4DP
        机器人:layout_weight =0.50
        机器人:背景=#ccbb22/>    <的LinearLayout
        机器人:ID =@ + ID / LinearLayout_chart
        机器人:layout_width =match_parent
        机器人:layout_height =0dp
        机器人:layout_margin =4DP
        机器人:layout_weight =0.50
        机器人:背景=#ddaaaa
        机器人:方向=横向>        <! - 你可以在这里你的图表 - >
    < / LinearLayout中>
< / LinearLayout中>

在这里输入的形象描述


  

风景模式


 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:layout_margin =4DP
    机器人:背景=#22bbcc
    机器人:方向=横向>    < ListView控件
        机器人:ID =@ + ID /的ListView
        机器人:layout_width =0dp
        机器人:layout_height =match_parent
        机器人:layout_margin =4DP
        机器人:layout_weight =0.50
        机器人:背景=#ccbb22/>    <的LinearLayout
        机器人:ID =@ + ID / LinearLayout_chart
        机器人:layout_width =0dp
        机器人:layout_height =match_parent
        机器人:layout_margin =4DP
        机器人:layout_weight =0.50
        机器人:背景=#ddaaaa
        机器人:方向=横向>        <! - 你可以在这里你的图表 - >
    < / LinearLayout中>< / LinearLayout中>

在这里输入的形象描述

如果有可能添加饼图动态的 LinerLayout

 私人饼图mChart;
私人浮子[] YDATA = {5,4,5,2};
私有String [] =外部数据{Item_one,Item_two,Item_Three,Item_Four};
linearLayoutTwo =(的LinearLayout)findViewById(R.id.LinearLayout_chart);
mChart =新饼图(本);    //配置饼图
    mChart.setUsePercentValues​​(真);
    mChart.setDescription();
    //使孔和配置
    mChart.setDrawHoleEnabled(假);
    mChart.setHoleColorTransparent(真);
    mChart.setHoleRadius(0);
    mChart.setTransparentCircleRadius(0);    //启用触摸图表的旋转
    mChart.setRotationAngle(0);
    mChart.setRotationEnabled(假);    ArrayList的<钥匙进入GT; yVals1 =新的ArrayList<钥匙进入GT;();    的for(int i = 0; I< yData.length;我++)
        yVals1.add(新条目(YDATA [I],I));    ArrayList的<串GT; xVals =新的ArrayList<串GT;();    的for(int i = 0; I< xData.length;我++)
        xVals.add(XDATA [I]);    //创建扇形数据集
    PieDataSet数据=新PieDataSet(yVals1,);
    dataSet.setSliceSpace(0);
    dataSet.setSelectionShift(1);    //添加多种颜色
    ArrayList的<整数GT;颜色=新的ArrayList<整数GT;();    对于(INT C:新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(颜色);    //实例现在馅饼数据对象
    PieData数据=新PieData(xVals,数据集);
    data.setValueFormatter(新PercentFormatter());
    data.setValueTextSize(9F);
    data.setValueTextColor(Color.BLACK);    mChart.setData(数据);    //撤消所有亮点
    mChart.highlightValues​​(NULL);    //更新饼图
    mChart.invalidate();
    linearLayoutTwo.addView(mChart);
    //定制的传说
    联想L = mChart.getLegend();    l.setPosition(Legend.LegendPosition.RIGHT_OF_CHART);
    l.setXEntrySpace(4);
    l.setYEntrySpace(4);

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

I am trying to set a layout for an activity in my android app. This activity consist on 2 elements, a ListView and a MPAndroidChart.

What I would like to achieve is to place the ListView at top, taking the height that it needs (wrap_content) and below it the chart with a fixed height.

I'm newbie on android, so the first thing I made was make a RelativeLayout with the list on the top and below that, the chart. This seemed fine in 'portrait' (the list had few element) but when I tried it on 'landscape' the chart disappeared...

So then I made a ScrollView wrapping the whole content (that it's a RelativeLayout) but I experimented some strange behavior (I guess because of the dynamic height of the listview). But anyway, the idea of having two different 'scrolls' (one for the list, another one for the whole activity) doesn't seem very nice.

Well, what I'm doing now is divide the height in two and have on the top part, the list, and on the bottom, the chart, like this:

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

But I don't really like it.

So, any thoughts on how to do this?

解决方案

Try this code

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>

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>

if possible add the PieChart dynamically to the 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);

这篇关于列表视图与MPAndroidChart(或其他)以下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆