以编程方式禁用 ScrollView?
我想启用 ScrollView 并通过单击按钮禁用它。
禁用意味着如果 ScrollView 不存在,则启用它会返回 ScrollView。
我想要这样,因为我有一个包含文本图像的画廊,并且单击按钮会更改屏幕方向,因此在横向中文本会变大。我想要 ScrollView 这样图像就不会自行拉伸并且文本变得不可读。
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>
我不能使用可见性(消失),因为这也会隐藏图库,我想要的是隐藏 ScrollView 的效果。当有ScrollView时,Gallery中的图像变得可滚动并且不适合屏幕,因此您必须滚动才能看到整个图像。我不想通过单击按钮来禁用/启用它。
我尝试了这个:
((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);
但是图库中的图像仍然是可滚动的并且不适合屏幕。解决这个问题的办法是什么?
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.
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)
doesn't make anything.
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>
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.
I tried this:
((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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
首先要注意的几点:
onTouchEvent
方法,以便在匹配某些条件时返回false
。android:scaleType
属性 (XML) 或setScaleType
方法更改 ImageView 的缩放方式 - 例如ScaleType.CENTER
不会拉伸您的图像,并将其以原始大小居中您可以按如下方式修改
ScrollView
以禁用滚动,使用
然后您将在 XML 文件中 (刚刚更改)
ScrollView
到您的特殊LockableScrollView
)。然后调用
以禁用视图的滚动。
我认为您遇到的不仅仅是禁用滚动的问题,以达到您想要的结果(例如,画廊将使用上面的代码保持可滚动) - 我建议对三个组件中的每一个进行更多研究(画廊, ScrollView、ImageView)来查看每个视图具有哪些属性及其行为方式。
Several points to begin with:
onTouchEvent
method to returnfalse
when some condition is matched.android:scaleType
property (XML) or thesetScaleType
method - for instanceScaleType.CENTER
will not stretch your image and will center it at it's original sizeYou could modify
ScrollView
as follows to disable scrollingYou would then use
in your XML file (just changed the
ScrollView
to your specialLockableScrollView
).Then call
to disable scrolling of the view.
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.
我是这样走的:
I had gone this way:
发现这个简单的解决方案刚刚设置
Found this simple solution just set
禁用ScrollView
Disablend ScrollView
我没有足够的观点来评论答案,但我想说 mikec 的答案对我有用,只是我必须将其更改为 return !isScrollable ,如下所示:
I don't have enough points to comment on an answer, but I wanted to say that mikec's answer worked for me except that I had to change it to return !isScrollable like so:
首先,我使用了第一个评论中发布的代码,但我将其更改如下:
然后我通过这种方式调用它,
因为我尝试过,
但它说 setIsScrollable 未定义,
我希望这会有所帮助你
to start, i used the Code posted posted in the first Comment but i changed it like this:
then i called it in by this way
because i tried
but it said that setIsScrollable is undefined
i hope this will help you
这是一个更简单的解决方案。如果您想通过触摸绕过滚动,请覆盖
ScrollView
OnTouchListener
的onTouch()
并返回 false。编程滚动仍然有效,无需扩展ScrollView
类。Here is a simpler solution. Override the
onTouch()
for theScrollView
OnTouchListener
and return false if you want to bypass the scroll by touch. The programmatic scrolling still works and no need to extend theScrollView
class.我在 NestedScrollView 上有一个布局,消耗触摸事件并禁用滚动:
并启用:
I had a layout over the NestedScrollView consuming the touch event and disable the scroll:
and to enable:
扩展
ScrollView
并重写onInterceptTouchEvent
以返回false
,在 Kotlin 中:这就够了!
(与使用
setOnTouchListener
相比,优点是这样禁用的ScrollView
不会以任何方式干扰后代的触摸事件处理)Extend
ScrollView
and overrideonInterceptTouchEvent
to returnfalse
, in Kotlin:That's enough!
(The advantage over using
setOnTouchListener
is that this way the disabledScrollView
does not interfere with touch event handling of descendants in any way)@JosephEarl +1 他在这里有一个很好的解决方案,对我来说非常适合,只需进行一些小的修改即可以编程方式完成。
以下是我所做的细微更改:
LockableScrollView Class:
MainActivity:
@JosephEarl +1 He has a great solution here that worked perfectly for me with some minor modifications for doing it programmatically.
Here are the minor changes I made:
LockableScrollView Class:
MainActivity:
您可以创建一个 CustomScrollView,为此您可以禁用它与其他视图的干扰。尽管这有时会让最终用户感到恼火。请谨慎使用。
这是代码:
如何使用CustomScrollView?
您可以将 CustomScrollView 作为父视图添加到要在其中添加另一个 Scrollview 作为子视图的屏幕,并且整个屏幕都是可滚动的。我将其用于相对布局。
它对我的案例有用。
You can make a CustomScrollView, for which you can disable its interference with other views. Though this can be sometimes irritating for the end user. Use it with caution.
This is the code:
How to use the CustomScrollView?
You can add CustomScrollView as a parent to the screen in which you want to add another Scrollview as a child view, and the whole screen is scrollable. I used this for a RelativeLayout.
It worked for my case.
科特林:
Kotlin:
这有帮助吗?
Does this help?
您可以扩展图库并在需要时使用一些标志来禁用滚动:
You can extend the gallery and use some flag to disable scrolling when you want:
这是 Kotlin 扩展
Here is a Kotlin extension
在按钮单击监听器上只需
这样做,以便在按钮单击时不启用滚动条
on button click listener just do
so that scroll bar is not enabled on button click
正如您在文档中看到的,您无法将可见性设置为 false。在你的情况下,你可能应该使用:
As you can see in the documentation, you cannot set the visibility to false. In your case you should probably use: