MP android图表线图布局参数

时间:2018-06-14 06:18:45

标签: android android-layout kotlin mpandroidchart

我想绘制12个折线图。所以我在scrollview中放置了一个线性布局。我将使用12个图表填充线性布局。我希望每个图表的高度等于屏幕的宽度(因为我在横向模式下查看图形)。我的代码是这样的:

class MainActivity : AppCompatActivity() {

private var screenHeight = 0
private var screenWidth = 0

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
    setContentView(R.layout.activity_main)

    val displayMetrics = DisplayMetrics()
    windowManager.defaultDisplay.getMetrics(displayMetrics)
    screenWidth = displayMetrics.widthPixels
    screenHeight = displayMetrics.heightPixels
    Log.i("width", screenWidth.toString())
    Log.i("height", screenHeight.toString())

    val chartHeight = screenHeight / 12
    for (i in 0..11){
        val lineChart = LineChart(this)
        lineChart.layoutParams.height = chartHeight  //line 33 giving error
        linearLayout.addView(lineChart)
    }
}
}

我遇到以下问题:

  1. 我想将线条图的宽度设为LayoutParams.match_parent但是当IDE要求我导入类LayoutParams时,我得到的选项是RelativeLayoutFrameLayoutViewGroup以及其他许多人,但其中任何一个与LineChart无关。

  2. 我提到this但它不适用于LineChart。我不知道为什么。我收到此错误:

  3.   

    AndroidRuntime:FATAL EXCEPTION:主要                                                                                  处理:atharva.example.com.graphlist,PID:29323                                                                                  java.lang.RuntimeException:无法启动活动   ComponentInfo {atharva.example.com.graphlist / atharva.example.com.graphlist.MainActivity}:   java.lang.NullPointerException:尝试写入字段' int   android.view.ViewGroup $ LayoutParams.width'在null对象上   参考                                                                                      在   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3319)                                                                                      在   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)                                                                                      在   android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5476)                                                                                      在android.app.ActivityThread.access $ 1200(ActivityThread.java:229)                                                                                      在   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1827)                                                                                      在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                      在android.os.Looper.loop(Looper.java:148)                                                                                      在android.app.ActivityThread.main(ActivityThread.java:7329)                                                                                      at java.lang.reflect.Method.invoke(Native Method)                                                                                      在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1230)                                                                                      在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

         

    引起:java.lang.NullPointerException:尝试写入字段' int android.view.ViewGroup $ LayoutParams.width'在null对象上   参考                                                                                       at atharva.example.com.graphlist.MainActivity $ override.onCreate(MainActivity.kt:33)                                                                                      在   atharva.example.com.graphlist.MainActivity $ override.access $讯(MainActivity.kt)                                                                                       at atharva.example.com.graphlist.MainActivity.onCreate(MainActivity.kt:0)                                                                                      在android.app.Activity.performCreate(Activity.java:6904)                                                                                      在   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)                                                                                      在   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266)

    1. 第0行的错误(参考上述错误日志)是什么意思?
    2. 请帮我设置布局参数,通过kotlin代码自定义视图,如MP Android LineChart

1 个答案:

答案 0 :(得分:1)

您没有设置图表的宽度值。尝试创建新的LayoutParams对象。这对我有用。

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams([your_width], [your_height]);
lineChart.setLayoutParams(params);

希望它有所帮助。