以编程方式禁用 ScrollView? [英] Disable ScrollView Programmatically?

查看:26
本文介绍了以编程方式禁用 ScrollView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想启用 ScrollView 并通过单击按钮禁用它.
禁用意味着如果 ScrollView 不存在,并启用它返回 ScrollView.

I would like to enable ScrollView and disable it by a Button Click.
Disable means like if the ScrollView wasn't there, and enable it returns the ScrollView.

我想要这样,因为我有一个带有文本图像的画廊,并且在一个按钮上单击屏幕方向会发生变化,因此在横向中文本会变大.而且我想要 ScrollView,这样图像就不会自行拉伸并且文本变得不可读.

I want that because I have a gallery with text images, and on a button click the screen orientation changes, so in Landscape the text becomes bigger. And I want the ScrollView so the image does not stretch itself and the text becomes unreadable.

scrollview.Enabled=false/setVisibility(false) 不做任何事情.

xml:

<ScrollView 
android:id="@+id/QuranGalleryScrollView" 
android:layout_height="fill_parent" 
android:layout_width="fill_parent">
<Gallery android:id="@+id/Gallery" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:scrollbars="horizontal"></Gallery>
</ScrollView>

我不能使用 Visibility (gone) 因为这也会隐藏画廊,我想要的是隐藏 ScrollView 的效果.当有 ScrollView 时,图库中的图像变得可滚动并且不适合屏幕,因此您必须滚动才能看到整个图像.我不想在单击按钮时禁用/启用它.

I can't use Visibility (gone) since that would also hide the Gallery, what I want is to hide the effect of the ScrollView. When there is ScrollView, the images in Gallery become scrollable and do not fit in the screen so you have to scroll to see the whole image. I don't want to disable/enable that on a button click.

我试过了:

((ScrollView)findViewById(R.id.QuranGalleryScrollView)).setOnTouchListener(null);
                        ((ScrollView)findViewById(R.id.QuranGalleryScrollView)).setHorizontalScrollBarEnabled(false);
                        ((ScrollView)findViewById(R.id.QuranGalleryScrollView)).setVerticalScrollBarEnabled(false);
                        ((ScrollView)findViewById(R.id.QuranGalleryScrollView)).setEnabled(false);

但图库中的图像仍然可以滚动并且不适合屏幕.有什么办法解决这个问题?

But still the images in the Gallery are scrollable and not fit the screen. What's the solution to this?

推荐答案

几点开始:

  1. 您不能禁用 ScrollView 的滚动.您需要扩展到 ScrollView 并覆盖 onTouchEvent 方法以在匹配某些条件时返回 false.
  2. 无论是否在 ScrollView 中,Gallery 组件都会水平滚动 - ScrollView 仅提供垂直滚动(水平滚动需要 Horizo​​ntalScrollView)
  3. 您似乎说图像拉伸本身有问题——这与 ScrollView 无关,您可以使用 android:scaleType 属性 (XML) 更改 ImageView 的缩放方式或 setScaleType 方法 - 例如 ScaleType.CENTER 不会拉伸您的图像并将其居中为其原始大小
  1. You cannot disable the scrolling of a ScrollView. You would need to extend to ScrollView and override the onTouchEvent method to return false when some condition is matched.
  2. The Gallery component scrolls horizontally regardless of whether it is in a ScrollView or not - a ScrollView provides only vertical scrolling (you need a HorizontalScrollView for horizontal scrolling)
  3. You seem to say you have a problem with the image stretching itself -- this has nothing to do with the ScrollView, you can change how an ImageView scales with the android:scaleType property (XML) or the setScaleType method - for instance ScaleType.CENTER will not stretch your image and will center it at it's original size

您可以修改ScrollView如下以禁用滚动

You could modify ScrollView as follows to disable scrolling

class LockableScrollView extends ScrollView {

    ...

    // true if we can scroll (not locked)
    // false if we cannot scroll (locked)
    private boolean mScrollable = true;

    public void setScrollingEnabled(boolean enabled) {
        mScrollable = enabled;
    }

    public boolean isScrollable() {
        return mScrollable;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // if we can scroll pass the event to the superclass
                return mScrollable && super.onTouchEvent(ev);
            default:
                return super.onTouchEvent(ev);
        }
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        // Don't do anything with intercepted touch events if
        // we are not scrollable
        return mScrollable && super.onInterceptTouchEvent(ev);
    }

}

然后你会使用

<com.mypackagename.LockableScrollView 
    android:id="@+id/QuranGalleryScrollView" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent">

    <Gallery android:id="@+id/Gallery" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:scrollbars="horizontal">
    </Gallery>

</com.mypackagename.LockableScrollView>

在您的 XML 文件中(只需将 ScrollView 更改为您的特殊 LockableScrollView).

in your XML file (just changed the ScrollView to your special LockableScrollView).

然后调用

((LockableScrollView)findViewById(R.id.QuranGalleryScrollView)).setScrollingEnabled(false);

禁用视图的滚动.

我认为您不仅仅是禁用滚动来实现您想要的结果的问题(例如,使用上述代码库将保持可滚动) - 我建议对三个组件中的每一个进行更多研究(图库、ScrollView、ImageView)以查看每个属性具有哪些属性以及它的行为方式.

I think that you have more than just the issue of disabling scrolling though to achieve your desired result (the gallery will remain scrollable with the above code for instance) - I'd recommend doing some more research on each of the three components (Gallery, ScrollView, ImageView) to see what properties each one has and how it behaves.

这篇关于以编程方式禁用 ScrollView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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