如何向ScrollView滚动条添加边距?

时间:2019-06-21 23:02:08

标签: android xml

这是我的ScrollView,其滚动条如下:

enter image description here

这里是style

<style name="ScrollBarStyle">
    <item name="android:height">100dp</item>
    <item name="android:scrollbarAlwaysDrawVerticalTrack">true</item>
    <item name="android:scrollbarStyle">insideInset</item>
    <item name="android:scrollbars">vertical</item>
    <item name="android:fadeScrollbars">false</item>
    <item name="android:scrollbarThumbVertical">@drawable/scrollbar_thumb</item>
    <item name="android:scrollbarTrackVertical">@drawable/scrollbar</item>
    <item name="android:scrollbarSize">12dp</item>
    <item name="android:paddingRight">20dp</item>
    <item name="android:layout_marginRight">20dp</item>
    <item name="android:scrollbarFadeDuration">2000</item>
    <item name="android:scrollbarDefaultDelayBeforeFade">1000</item>
</style>

xml

<ScrollView
    android:id="@+id/profileScroll"
    style="@style/ScrollBarStyle"
    android:fillViewport="true"
    android:background="@drawable/curved_profile"
    app:layout_constraintVertical_weight="0.75"
    android:elevation="10dp"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_marginStart="30dp"
    android:layout_marginEnd="30dp"
    android:layout_marginTop="10dp">

drawable / scrollbar.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@android:color/transparent" />
            <stroke android:color="@color/colorPrimary" android:width="1dp"/>
            <size android:height="100dp" android:width="15dp"/> // this size attribute doesn't change the size of the scrollbar
        </shape>
    </item>
</selector>

如何在滚动条和ScrollView之间添加边距?因此,它不在很远的地方,而是更中心。如您在我的style中所看到的,我已经尝试了边距和填充,但是它们无法正常工作。

我还想减小滚动条的高度,使其仅为封闭的ScrollView的一半。

我该如何实现?

1 个答案:

答案 0 :(得分:0)

在我的情况下有效:

<androidx.constraintlayout.widget.ConstraintLayout 
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     tools:context=".MainActivity">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_blue_light"
    android:layout_marginEnd="16dp"
    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