在 ScrollView 中隐藏滚动条

我有一个带有 ScrollView 的应用程序,我不希望滚动条出现在屏幕上。如何在 ScrollView 中隐藏滚动条,同时确保滚动仍然有效?

enter image description here

102978 次浏览

In Java add this code:

myScrollView.setVerticalScrollBarEnabled(false);
myScrollView.setHorizontalScrollBarEnabled(false);

In XML add following attribute to your ScrollView:

android:scrollbars="none"

Like this:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/mainScroll"
android:scrollbars="none" <!-- line to be added -->
>

In XML set android:scrollbars="none"

In the XML layout, add this property:

android:scrollbarSize="0dp"

Try this, it is also working...

android:scrollbarThumbVertical="@null"

or

android:scrollbarThumbHorizontal="@null"

This will hide the Scroll bar stick but scroll bar is not disable

android:scrollbarThumbVertical="@null"


android:scrollbarThumbHorizontal="@null"

This will disable the scroll bar

android:scrollbars="none"

For hiding a vertical scrollbar, do this in the XML:

android:scrollbarThumbVertical="@null"

And for Hiding horizontal scrollbar do this :

android:scrollbarThumbHorizontal="@null"

The above lines of codes will work if you want to hide the scrollbar without disabling it.

And for disabling a scrollbar, write this:

android:scrollbars="none"

In my experience,

android:scrollbarThumbVertical="@null"

can cause NullPointerException in older devices. Use this instead:

android:scrollbarThumbVertical="@android:color/transparent"

Cheers!

you have to try the following solutions

    android:scrollbars="none"

OR

    android:scrollbarThumbVertical="@null"
android:scrollbarThumbHorizontal="@null"

OR Change color of scrollBars to hide them

    android:scrollbarThumbVertical="@android:color/transparent"

Now the scroll does not work anymore if u set android:scrollbars="none"

I have solved the problem with

  android:scrollbars="vertical" // or horizontal

and setting its size to 0 dp

  android:scrollbarSize="0dp"

If you are making "custom" HorizontalScrollView then you should set those properties in code like so

this.scrollBarSize = 0 this.isHorizontalScrollBarEnabled = false

That is the only way I got mine to work.

Kotlin Solution

If you need to do this programmatically, you can set either one or both of:

scrollView.isHorizontalScrollBarEnabled = false
scrollView.isVerticalScrollBarEnabled = false

If you'll be applying both regularly, try adding this extension

fun ScrollView.noScrollbars() {
isHorizontalScrollBarEnabled = false
isVerticalScrollBarEnabled = false
}

To easily allow switching, you can add an optional boolean

fun ScrollView.noScrollbars(hide: Boolean = true) {
isHorizontalScrollBarEnabled = !hide
isVerticalScrollBarEnabled = !hide
}