下面带有 MPAndroidChart(或其他)的列表视图
Listview with a MPAndroidChart(or whatever) below
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,其中列表位于图表的顶部和下方。这在 'portrait' 中看起来不错(列表中的元素很少),但是当我在 'landscape' 中尝试时,图表消失了...
然后我制作了一个包含整个内容的 ScrollView(这是一个 RelativeLayout),但我尝试了一些奇怪的行为(我猜是因为列表视图的动态高度)。但是无论如何,有两个不同的'scrolls'(一个用于列表,另一个用于整个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>
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>
如果可能,将 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);
我终于找到了满足我要求的解决方案。
我post这个解决方案,也许对某人有帮助。
我所做的只是将图表作为 ListView 的页脚插入,所以我只有 listview 的滚动和最后 de MPAndroidChart(如我所愿)。
为此我制作了一个新的布局文件,chartlayout.xml
:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="match_parent">
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/chart"
android:layout_width="match_parent"
android:layout_height="200dp"/>
</RelativeLayout>
所以在我的 activity java 文件中,我使用 inflater 服务从 chartlayout
中获取布局,然后我得到图表视图,最后我将它添加到列表中页脚:
//get layout
chartLayout = ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.chartlayout, null, false);
//get view
mChart = (LineChart) chartLayout.findViewById(R.id.chart);
/*
* chart stuff
*/
...
mylistView.addFooterView(chartLayout);
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,其中列表位于图表的顶部和下方。这在 'portrait' 中看起来不错(列表中的元素很少),但是当我在 'landscape' 中尝试时,图表消失了...
然后我制作了一个包含整个内容的 ScrollView(这是一个 RelativeLayout),但我尝试了一些奇怪的行为(我猜是因为列表视图的动态高度)。但是无论如何,有两个不同的'scrolls'(一个用于列表,另一个用于整个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>
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>
如果可能,将 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);
我终于找到了满足我要求的解决方案。
我post这个解决方案,也许对某人有帮助。
我所做的只是将图表作为 ListView 的页脚插入,所以我只有 listview 的滚动和最后 de MPAndroidChart(如我所愿)。
为此我制作了一个新的布局文件,chartlayout.xml
:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="match_parent">
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/chart"
android:layout_width="match_parent"
android:layout_height="200dp"/>
</RelativeLayout>
所以在我的 activity java 文件中,我使用 inflater 服务从 chartlayout
中获取布局,然后我得到图表视图,最后我将它添加到列表中页脚:
//get layout
chartLayout = ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.chartlayout, null, false);
//get view
mChart = (LineChart) chartLayout.findViewById(R.id.chart);
/*
* chart stuff
*/
...
mylistView.addFooterView(chartLayout);