gpt4 book ai didi

android - 减少回收站 View 中滚动条的滚动区域/大小

转载 作者:行者123 更新时间:2023-12-05 04:25:36 25 4
gpt4 key购买 nike

我想减少回收器 View 的滚动条大小(附图),我尝试为 Thumb & Track 创建自定义可绘制对象,但拇指不在轨道中。有人可以帮助我更好地实现这一目标吗?

enter image description here

在上图中,底部的水平滚动条尺寸较小。我想实现这样的目标。

enter image description here

最佳答案

这是一种您可以做您想做的事情的方法:

定义拇指:

enter image description here

custom_thumb.xml

<shape
android:shape="rectangle">
<corners android:radius="20dp" />
<solid android:color="@android:color/holo_red_light" />
<size
android:width="40dp"
android:height="10dp" />
</shape>

定义搜索栏的可绘制进度:

seekbar_drawable.xml

<layer-list>
<item android:id="@android:id/progress">
<clip android:drawable="@android:color/transparent" />
</item>
</layer-list>

我们不会显示搜索栏的进度,因此我们将进度绘制设置为“透明”。我们将定义另一个显示在进度条后面的 View ,而不是进度条。我们为什么要做这个?这是因为搜索栏想要将拇指移到进度区域之外,而我们希望拇指完全保持在进度区域内。第二个 View 包含所有搜索栏以及左侧和右侧的一些区域以包含拇指。

seekbar_background.xml

<shape>
<corners android:radius="20dp"/>
<solid android:color="@android:color/darker_gray" />
</shape>

下面是一些示例 XML 来展示它的外观:

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">

<View
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@drawable/seekbar_background"
app:layout_constraintBottom_toBottomOf="@id/seekBar"
app:layout_constraintEnd_toEndOf="@id/seekBar"
app:layout_constraintStart_toStartOf="@id/seekBar"
app:layout_constraintTop_toTopOf="@id/seekBar" />

<SeekBar
android:id="@+id/seekBar"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:max="10000"
android:min="0"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:progressDrawable="@drawable/seekbar_drawable"
android:thumb="@drawable/custom_thumb"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

enter image description here

使用 SeekBar.OnSeekBarChangeListener使用 RecyclerView 的 滚动功能滚动 RecyclerView。当手动滚动 RecyclerView 时,使用 RecyclerView.OnScrollListener设置搜索栏的进度。您将必须建立两个 View 滚动状态的映射。

我已经硬编码了一些您可能需要调整的值。

可以使用以下代码以编程方式更改缩略图的宽度,以根据 Recyclerview 项的数量或任何其他值进行调整。

例如,要将拇指的宽度更改为原来的两倍,您可以执行以下操作:

val thumb = binding.seekBar.thumb as GradientDrawable
thumb.setSize(thumb.intrinsicWidth * 2, thumb.intrinsicHeight)

关于android - 减少回收站 View 中滚动条的滚动区域/大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73208154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com