Android:制作可滚动的自定义视图 [英] Android: make a scrollable custom view

查看:41
本文介绍了Android:制作可滚动的自定义视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经滚动了自己的自定义视图,可以绘制到屏幕上,但是我真正想做的是将屏幕的measuredHeigh设置为1000px,然后让用户在Y轴上滚动,但我在执行此操作时遇到了问题.有人可以帮忙吗?

I've rolled my own custom view and can draw to the screen alright, but what I'd really like to do is set the measuredHeigh of the screen to, say, 1000px and let the user scroll on the Y axis, but I'm having problems doing this. Can anyone help?

以下是一些代码:

public class TestScreen extends Activity  {
     CustomDrawableView mCustomDrawableView;
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);    
         mCustomDrawableView = new CustomDrawableView(this);
         setContentView(mCustomDrawableView);
     }
 }

public class CustomDrawableView extends View {

    public CustomDrawableView(Context context) {
        super(context);
        setVerticalScrollBarEnabled(true);
        setMinimumHeight(1000);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawLine(...);
        // more drawing
    }
}

我试图通过调用super来覆盖scrollTo,scrollBy,awakenScrollBars等,但无济于事.我是不是很想念某些东西,还是犯了一些根本性的错误?

I've tried to override scrollTo, scrollBy, awakenScrollBars etc with a call to super but to no avail. Am I missing something silly, or am I making some fundamental mistake?

先谢谢您

Martyn

添加:

我尝试使用以下布局文件将其添加为自定义组件,并使用 setContentView(R.layout.exampleLayout)将 TestScreen 中的代码更改为指向正确的资源,但这会导致模拟器崩溃.我尝试将代码注释到最低限度,但仍然崩溃,因此我正在做一些根本错误的事情,但是我不确定它是什么:

I've tried to add this as a custom component with the below layout file and changed the code in TestScreen to point at the correct resource with setContentView(R.layout.exampleLayout), but this causes the emulator to crash. I tried commenting the code down to the bare minimum and it still crashes, so there's something fundamentally wrong that I'm doing but I'm not sure what it is :

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content">
       <ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent">
            <com.martyn.testApp.CustomDrawableView
               android:layout_width="fill_parent"
               android:layout_height="fill_parent"
             />
       </ScrollView>
</LinearLayout>

推荐答案

只需将视图放入ScrollView!

Just put your view in a ScrollView!

请注意,ScrollWiew应该是根节点(这是您的LinearLayout)

Note that the ScrollWiew should be the root node (here it's your LinearLayout)

这篇关于Android:制作可滚动的自定义视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆