android开发入门教程

android开发入门教程
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
问题描述
我想自定义一个View,利用Canvas画一个折线图,但折线图有点长,单凭手机屏幕的宽度无法显示完全,所以我想在View的上层在加一个Horizontalscrollview控件,这样可以水平滑动查看折线图,可问题是添加完Horizontalscrollview控件后就不显示我画的图了。

开始能显示的Layout布局如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    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"

    tools:context=".MyChartLine"

    android:orientation="vertical"

    >



    <LinearLayout

        android:background="#f4f4f7"

        android:layout_height="match_parent"

        android:layout_width="match_parent"

        android:orientation="vertical">

            <RelativeLayout

                android:id="@+id/linear"

                android:layout_height="match_parent"

                android:layout_marginTop="30dp"

                android:layout_width="wrap_content"

                >



            </RelativeLayout>

    </LinearLayout>

</LinearLayout>


在主函数引用如下:

package com.example.dell.drawchartline;



import android.app.Activity;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.widget.HorizontalScrollView;

import android.widget.LinearLayout;

import android.widget.RelativeLayout;



import java.util.ArrayList;

import java.util.List;







public class MyChartLine extends Activity {



    private RelativeLayout linear;

    //private LinearLayout linear;

    //private HorizontalScrollView linear;



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_my_chart_line);

        List<Integer> list = new ArrayList<Integer>();

        list.add(65);

        list.add(78);

        list.add(65);

        list.add(43);

        list.add(62);

        list.add(34);

        list.add(45);

        list.add(24);

        list.add(23);

        list.add(56);

        list.add(86);



        List<String> Data = new ArrayList<String>() ;

        Data.add("09-01-6-05");

        Data.add("09-01-6-05");

        Data.add("09-01-6-05");

        Data.add("09-01-6-05");

        Data.add("09-01-6-05");

        Data.add("09-01-6-05");

        Data.add("09-01-6-05");

        Data.add("09-01-6-05");

        Data.add("09-01-6-05");

        Data.add("09-01-6-05");

        linear = (RelativeLayout) findViewById(R.id.linear);

        linear.addView(new BrokeLine(this,Data,list));

    }

}


显示效果为:


但添加Horizontalscrollview空间后:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    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"

    tools:context=".MyChartLine"

    android:orientation="vertical"

    >



    <LinearLayout

        android:background="#f4f4f7"

        android:layout_height="match_parent"

        android:layout_width="match_parent"

        android:orientation="vertical">





        <HorizontalScrollView

            android:layout_width="match_parent"

            android:layout_height="match_parent">



            <RelativeLayout

                android:id="@+id/linear"

                android:layout_height="match_parent"

                android:layout_marginTop="30dp"

                android:layout_width="wrap_content"

                >



            </RelativeLayout>



        </HorizontalScrollView>









    </LinearLayout>

</LinearLayout>


画的图就不显示了,
很是奇怪!求教大神?? 解决方案1
原因正如我上面所说的,你没有重写 onMeasure 方法,而且你的 RelativeLayout 的布局是 wrap_content 的宽度,这样, RelativeLayout 在测量子布局的时候使用 View 默认的 onMeasure 方法,获得的测量宽高为0,所以不显示,你可以给你的 BrokenLine 加上 LayoutParmas, 然后就有显示了,或者重写 onMeasure 方法,自己设定宽高

BrokeLine line = new BrokeLine(this,s,i);

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(400, ViewGroup.LayoutParams.MATCH_PARENT);

        line.setLayoutParams(params);
我要啦免费统计