MPAndroidchartlibrary如何调整片段中条形图的高度

时间:2016-11-19 21:04:09

标签: android android-fragments mpandroidchart

我无法设置条形图的大小

enter image description here

我在活动中的片段内有图表。

如果我使用setContentView(图表),它将填满整个屏幕,但我想限制它。

这里是放置图表的XML :(更改layout_height没有帮助)

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.github.mikephil.charting.charts.BarChart
    android:id="@+id/piechart"
    android:layout_weight="2"
    android:layout_width="fill_parent"
    android:layout_height="30dp">
</com.github.mikephil.charting.charts.BarChart>
</ScrollView>

这是片段:

package com.example.kim.s236322_mappe3.View;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.example.kim.s236322_mappe3.R;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.utils.ColorTemplate;

import java.util.ArrayList;


public class StatisticsFragment extends Fragment {
View view;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    view = inflater.inflate(R.layout.statistics_layout,container,false);
    ArrayList<BarEntry> entries = new ArrayList<>();
    entries.add(new BarEntry(4f, 0));
    entries.add(new BarEntry(8f, 1));
    entries.add(new BarEntry(6f, 2));
    entries.add(new BarEntry(12f, 3));
    entries.add(new BarEntry(18f, 4));
    entries.add(new BarEntry(9f, 5));

    BarDataSet dataset = new BarDataSet(entries, "# of Calls");

    ArrayList<String> labels = new ArrayList<String>();
    labels.add("January");
    labels.add("February");
    labels.add("March");
    labels.add("April");
    labels.add("May");
    labels.add("June");

    BarChart chart = new BarChart(getActivity().getBaseContext());
    BarChart layout = (BarChart) view.findViewById(R.id.piechart);
    layout.addView(chart);

    BarData data = new BarData(labels, dataset);
    chart.setData(data);

    chart.setDescription("# of times Alice called Bob");
    dataset.setColors(ColorTemplate.COLORFUL_COLORS);
    chart.animateY(5000);
    return view;
}

}

1 个答案:

答案 0 :(得分:2)

此处的解决方案非常简单,与MPAndroidChart无关。 将android:fillViewport="true"设置为ScrollView,以便填充高度并将android:layout_height="match_parent"设置为BarChart