【问题标题】:MPandroidChart Layout HeightMPandroidChart 布局高度
【发布时间】:2015-10-21 18:52:45
【问题描述】:

我正在使用 MPAndroidChart 在片段内创建条形图,但图表高度只是整个屏幕的一小部分。此外,当高度设置为数字时,它可以正常工作,但这不是我想要的解决方案。问题是什么? 下面是片段布局代码。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        tools:context=".MainActivity$PlaceholderFragment">

        <com.github.mikephil.charting.charts.BarChart
            android:id="@+id/barchart"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </RelativeLayout>

编辑:抱歉缺少详细信息,我为我的项目使用了带有操作栏微调器的选项卡式活动模板。

    <android.support.design.widget.AppBarLayout android:id="@+id/appbar"
    android:layout_width="match_parent" android:layout_height="wrap_content"
    android:paddingTop="@dimen/appbar_padding_top"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
        android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay"
        app:layout_scrollFlags="scroll|enterAlways">

        <Spinner android:id="@+id/spinner" app:popupTheme="@style/AppTheme.PopupOverlay"
            android:layout_width="wrap_content" android:layout_height="wrap_content" />
    </android.support.v7.widget.Toolbar>

</android.support.design.widget.AppBarLayout>

<android.support.v4.widget.NestedScrollView android:id="@+id/container"
    android:layout_width="match_parent" android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

我还有正在运行的应用程序的图像

Click to view the image

【问题讨论】:

  • 您到底想要发生什么?您已将RelativeLayout 的高度设置为wrap_content,因此可能在RelativeLayout 上方或下方还有其他视图也占用了空间。
  • 我已经尝试了“match_parent”和“fill_parent”,结果是一样的,我也没有添加任何其他视图

标签: android mpandroidchart


【解决方案1】:

我知道这是一个较老的问题,但我遇到了同样的问题 - 我以编程方式创建的图表根据需要是宽的,但高度不是从父 linearLayout 继承的。
解决方案涉及在将高度添加到父视图后以编程方式设置高度,然后重绘它(invalidate())。

布局:

<LinearLayout
android:id="@+id/short_performance_chart"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical"
android:layout_marginTop="20dp"
android:background="@drawable/graph_gradient"/>

代码:

LinearLayout chartHolder = (LinearLayout) getActivity().findViewById(R.id.short_performance_chart);
BarChart mChart = (BarChart) initializeBarChart(); //just populating data, etc
chartHolder.addView(mChart);
// Due to the fact that we are in a fragment we have to reset the height
mChart.getLayoutParams().height = android.view.ViewGroup.LayoutParams.MATCH_PARENT;
mChart.invalidate();

【讨论】:

  • 对我不起作用。在第一方,我已经无法正确渲染单个图表。不是一个好兆头。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-29
  • 1970-01-01
  • 2021-05-03
相关资源
最近更新 更多