简体   繁体   English

无法将CustomView添加到ScrollView

[英]cannot add CustomView to ScrollView

I want to display scrollable line graphe. 我想显示可滚动的线图。 I have maked CustomView which shows line graphe, then added this to ScrollView. 我制作了CustomView来显示线形图,然后将其添加到ScrollView中。 But CustomView don't be rendered. 但是CustomView不会被渲染。 Prease tell me what's wrong with following code. 请先告诉我以下代码有什么问题。 Thanks in advance. 提前致谢。

Activity: 活动:

public class MainActivity extends AppCompatActivity {

    private HorizontalScrollView horizontalScrollView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        System.out.println("*** MainActivity.onCreate - start");

        horizontalScrollView = (HorizontalScrollView) findViewById(R.id.horizontal_scrollview);

        horizontalScrollView.addView(new CustomView(this));
        System.out.println("*** MainActivity.onCreate - end");
    }
}

layout: 布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="org.macho.myapplication.MainActivity">

    <HorizontalScrollView
        android:id="@+id/horizontal_scrollview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </HorizontalScrollView>
</LinearLayout>

CustomView: CustomView:

public class CustomView extends View {
    public CustomView(Context context) {
        super(context);
        System.out.println("*** CustomView.constructor");
    }

    @Override
    public void onDraw(Canvas canvas) {
        System.out.println("*** CustomView.onDraw - start");
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setColor(Color.rgb(0, 0, 0));
        paint.setStrokeWidth(10);
        paint.setStyle(Paint.Style.STROKE);
        canvas.drawText("AAAAAAAAAABBBBBBBBBBCCCCCCCCCCDDDDDDDDDDEEEEEEEEEE", 0, 0, paint);
        View parent = (View) getParent();
        canvas.drawLine(0, 0, parent.getWidth(), parent.getHeight(), paint);
        System.out.println("*** CustomView.onDraw - end");
    }
}

console: 安慰:

11-19 22:25:00.867 26170-26170/org.macho.myapplication I/System.out: *** MainActivity.onCreate - start
11-19 22:25:00.884 26170-26170/org.macho.myapplication I/System.out: *** CustomView.constructor
11-19 22:25:00.885 26170-26170/org.macho.myapplication I/System.out: *** MainActivity.onCreate - end

你能像上面的文章一样从oncreate调用horizo​​ntalScrollView.invalidate()吗?

custom views using onDraw should override the onMeasure method to tell the parent what bounds it wants to be. 使用onDraw的自定义视图应覆盖onMeasure方法,以告诉父级它想要成为的边界。

this could look like: 可能看起来像:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int height = 500; // should be calculated based on the content
    int width = 1500; // should be calculated based on the content
    setMeasuredDimension(width, height);
}

make sure to take widthMeasureSpec and heightMeasureSpec into account. 确保将widthMeasureSpecheightMeasureSpec考虑在内。

see https://developer.android.com/training/custom-views/custom-drawing.html for more details 详情请参阅https://developer.android.com/training/custom-views/custom-drawing.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM