无法将CustomView添加到ScrollView

时间:2017-11-19 13:37:36

标签: android android-layout scrollview

我想显示可滚动的线条。我已经制作了CustomView,它显示了行graphe,然后将其添加到ScrollView。但CustomView不会被渲染。 Prease告诉我以下代码有什么问题。 提前谢谢。

的活动:

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");
    }
}

布局:

<?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:

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");
    }
}

控制台:

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

2 个答案:

答案 0 :(得分:0)

你可以从oncreate调用horizo​​ntalScrollView.invalidate(),如post

答案 1 :(得分:0)

使用onDraw的自定义视图应该覆盖onMeasure方法,以告诉父母它想要的边界。

这可能看起来像:

@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);
}

请务必考虑widthMeasureSpecheightMeasureSpec

有关详细信息,请参阅https://developer.android.com/training/custom-views/custom-drawing.html