自定义视图的ScrollView不会滚动 - android?

时间:2018-03-26 13:15:39

标签: java android view scrollview

尝试在ScrollView上构建和添加自定义视图。但它不会滚动。

虽然凌驾于测量之上 - 却无法识别。

<ScrollView 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:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

</ScrollView>
//tried above making as child.

代码:

ScrollView scrollView = findViewById(R.id.scrollView);
//LinearLayout linearLayout = findViewById(R.id.line);

myView gw = new myView(this,bitmap);
// ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(480,800);
scrollView.addView(gw);
scrollView.setFillViewport(true);

myView类使用以下代码绘制位图:

public class myView extends SurfaceView implements View.OnTouchListener {

    private Bitmap bitmap;

    private SurfaceHolder holder;

    public myView(Context context, Bitmap bitmap) {

        super(context);

        holder = getHolder();

        this.bitmap = bitmap;

 }
         .....

    @Override

    public void draw(Canvas canvas) {

        super.draw(canvas);

  canvas.drawBitmap(bitmap, 10, 20, null);


        canvas.drawPath(path, paint);


        canvas.drawText("Second floor", x, y + 600, paint1);

        Log.d("test", "drawing....");


    }

查看绘制但不会滚动。 如何滚动?

1 个答案:

答案 0 :(得分:0)

使用某些ViewGroup(如LinearLayout或FrameLayout:

)换行视图
LinearLayout linear = new LinearLayout(this);
linear.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
linear.setOrientation(1);
scrollView.addView(linear);

myView gw = new myView(this,bitmap);
linear.addView(gw );

编辑:

从您的代码中我看到您正在使用SurfaceView。 SurfaceView非常不同,不支持从ScrollView滚动。您可以将视图从SurfaceView更改为其他内容或添加:

implements View.OnTouchListener, SurfaceHolder.Callback

并自己实现滚动。

请参阅:Android SurfaceView scrolling