【问题标题】:Android: scrollbar track thinner than thumb in RecyclerViewAndroid:滚动条轨道比 RecyclerView 中的拇指更薄
【发布时间】:2020-09-15 12:58:16
【问题描述】:

如何在不使用自定义滚动视图的情况下将滚动条轨道设置为比 RecyclerView 中的拇指更细

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/rv"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:scrollbars="vertical"
    android:clickable="true"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:fadeScrollbars="false"
    android:scrollbarSize="4dp"
    android:scrollbarThumbVertical="@drawable/scroll_view_gradient"
    android:scrollbarTrackVertical="@color/scroll_bar_track"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

【问题讨论】:

  • 也许你可以使用一个 scrollbarTrackVertical 可绘制的图像,它是一个中心有细线,两边都是透明的图像。因此,即使“物理”轨道更大,但在视觉上它似乎更小。

标签: android android-recyclerview android-scrollview


【解决方案1】:

我自己做了这个,它看起来“不错”并且表现“有点不错”,但是有些东西没有经过足够的测试,至少应该有点帮助。也可以在this link 上查看。另外,这首先只是视觉上的,如果你想自定义拇指的长度,那么你需要创建一个自定义视图并操作偏移量。由于它不是问题的一部分(和这个答案),所以我没有写它,如果有人确实需要它可以在上面的链接中找到它。

bottom, left, right & top 是 insets,你可以把它们看成 paddings,它们不会影响布局大小,但是你应该保持 top & bottom insets 在轨道上,拇指在相同的大小(垂直recyclerviews,水平的应该使用左右)。 还可以控制拇指和轨迹的粗细,以及笔划宽度放置透明像素,使轨迹看起来比实际要小。感谢@Razvan S。 scrollbar_track.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:bottom="8dp"
        android:left="8dp"
        android:right="8dp"
        android:top="8dp">
        <shape>
            <solid android:color="@color/scroll_bar_color" />
            <stroke
                android:width="3dp"
                android:color="@android:color/transparent" />
            <size android:width="4dp"
                  android:height="4dp" />
        </shape>
    </item>
</layer-list>

这不需要width和height属性,因为recyclerview会自动计算长度,track width设置拇指宽度。 thumb_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:bottom="8dp"
        android:left="8dp"
        android:right="8dp"
        android:top="8dp">
        <shape android:shape="rectangle">
            <solid android:color="@color/scroll_bar_color" />
            <corners android:radius="4dp" />
        </shape>
    </item>
</layer-list>

styles.xml

<style name="scrollbar_style">
    <item name="android:scrollbarAlwaysDrawVerticalTrack">true</item>
    <item name="android:scrollbars">vertical</item>
    <item name="android:fadeScrollbars">false</item>
    <item name="android:scrollbarThumbVertical">@drawable/thumb_shape</item>
    <item name="android:scrollbarTrackVertical">@drawable/scrollbar_track</item>
</style>

你的布局中的recyclerview标签:style="@style/scrollbar_style"

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    style="@style/scrollbar_style" />

现在是第二部分,如果你想操纵拇指的长度,你需要自己计算偏移量。我通过创建一个实现 ScrollingViewCustom RecyclerView 来做到这一点,并手动计算所有内容。

希望这对您有所帮助,它为我完成了这项工作。

对于那些想知道的人,我使用 layer-list 是因为它可以按我的意愿工作,如果我切换到其他东西我没有得到想要的行为。它可能有点缺陷,并且可能在某个地方有更好的解决方案,但是我通过反复试验来做到这一点,直到我产生了可以使用的东西。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 2018-02-07
    • 1970-01-01
    • 2011-09-10
    • 1970-01-01
    • 1970-01-01
    • 2018-11-16
    相关资源
    最近更新 更多