簡體   English   中英

Android SeekBar 中不需要的填充

[英]Unwanted padding in Android SeekBar

我在使用 Android 搜索欄時遇到問題,搜索欄視圖似乎有默認填充,我似乎無法刪除它,我嘗試將填充設置為“0dp”並嘗試將邊距設置為“-10dp”以查看它是否刪除了一些空間並嘗試將背景設置為 null 以防背景占用空間但似乎沒有任何反應,SeekBar 標簽是:

<SeekBar
    android:paddingLeft="0px"
    android:paddingRight="0px"
    android:background="@null"
    android:id="@+id/musicSeekBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

編輯:好的,經過一些測試和嘗試一些東西后,我了解到:

  1. 填充是由於拇指可繪制大小
  2. 設置android:thumb="@null"將刪除底部和頂部填充
  3. 即使我移除拇指,左右填充也會保留,但可以通過在兩側設置負邊距等於默認拇指可繪制寬度除以 2 來移除,因此如果它的 32dp 每側為 16dp

現在我需要知道是否有更簡潔的方法來移除填充物,以及所有設備上的拇指大小是否相同? 歡迎任何幫助。

要從Seekbar中刪除填充,您必須在運行時執行以下操作:

findViewById(R.id.seekBar).setPadding(0,0,0,0);

====================== ======================

Bitmap thumbImage = BitmapFactory.decodeResource(getResources(), R.drawable.thumb);
int paddingDp = (int)(0.5f * (thumbImage.getWidth()));
findViewById(R.id.seekBar).setPadding(paddingDp,0,paddingDp,0);

您不需要設置android:thumb="@null"

只需將minHeight設置為0dp,它將“刪除”上方和下方的填充。

只需將這些行寫入您的 xml 代碼並根據您的要求更改值。

android:layout_marginStart="-16dp"
android:layout_marginEnd="-16dp"

我意識到這已經晚了3年了,但是對於像我這樣仍然無法找到該解決方案的任何人,這就是它。 問題是拇指可繪制對象添加了頂部/底部填充(嘗試將拇指可繪制@null設置為@null並觀察填充消失)。 解決方案是將所有內容偏移拇指可繪制對象的一半大小。 為確保布局在所有設備上保持一致,我們將創建一個自定義的拇指可繪制對象,以便控制大小。

第1步:創建自定義拇指可繪制對象。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape android:shape="oval">
            <size android:height="10dp" android:width="10dp" />
            <solid android:color="@color/mediaThumbColor" />
        </shape>
    </item>
</layer-list>

步驟2:設置SeekBar。

<SeekBar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:thumb="@drawable/media_seekbar_thumb"
    android:paddingStart="0dp"
    android:paddingEnd="0dp"
    android:splitTrack="false"
    android:thumbOffset="5dp"
    android:elevation="4dp"
    />
  • 將可繪制的拇指設置為我們剛剛創建的拇指。
  • 設置splitTrack=false可以解決自定義的拇指可繪制對象被填充為空白的問題。
  • 將開始/結束填充設置為0,以確保它是完整的父寬度(默認情況下,SeekBar具有16dp填充)。
  • 將拇指偏移設置為您使自定義拇指可繪制的任何大小的一半(在我們的示例中為10dp,因此拇指偏移為5dp)。
  • 給搜索欄一定的高度,以確保它顯示在背景內容之上(在下面說明)。

步驟3:設置媒體欄布局。

這是正確排列所有內容的關鍵。 媒體欄將由頂層父布局組成,其中包含(1)SeekBar,以及(2)內部父項,其中包含其他所有內容(播放/暫停,后退/前進,跟蹤時間等)。 頂級父級需要具有透明背景,並且具有高度wrap_content 內父母需要具有您想要媒體欄成為的任何背景顏色,以及一個您想要媒體欄成為的任何高度的高度。

將內部父級設置為與頂級父級在頂部對齊, 頂部邊距為自定義thumb drawable高度的一半 ,在本例中為5dp。 這會使媒體欄中的所有內容偏移,以使SeekBar看起來位於其頂部。

這是使用ConstraintLayout的示例。

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/media_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    >

    <SeekBar
        android:id="@+id/media_bar_seekbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:thumb="@drawable/media_seekbar_thumb"
        android:paddingStart="0dp"
        android:paddingEnd="0dp"
        android:splitTrack="false"
        android:thumbOffset="5dp"
        android:elevation="4dp"
        />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/media_bar_content"
        android:layout_width="match_parent"
        android:layout_height="@dimen/media_bar_height"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginTop="5dp"
        android:background="@color/mediaBarColor"
        >

        <!-- All mediabar content goes here -->

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

我的解決方案與上述類似,但我認為更短一些。 它只是基於這樣一個事實,即需要根據您在SeekBar元素上定義的屬性android:minHeightandroid:maxHeight重新定義 seekbar thumb drawable 的大小。 請注意使用值@dimen/seek_bar_height的位置。

最終的 SeekBar 如下所示:

在此處輸入圖像描述

組件本身:

<SeekBar
    android:id="@+id/seekBarAudio"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="@dimen/seek_bar_height"
    android:maxHeight="@dimen/seek_bar_height"
    android:paddingStart="0dp"
    android:paddingEnd="0dp"
    android:progressDrawable="@drawable/seekbar"
    android:thumb="@drawable/seekbar_thumb"/>

資源@drawable/seekbar簡單定義為:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<item android:id="@android:id/progress">
    <clip>
        <shape>
            <solid android:color="@color/colorAccent"/>
        </shape>
    </clip>
</item>

</layer-list>

@drawable/seekbar_thumb為:

<shape xmlns:android="http://schemas.android.com/apk/res/android">

<size
    android:width="@dimen/seek_bar_height"
    android:height="@dimen/seek_bar_height"/>

</shape>

盡管仍然可以手動選擇進度,但用戶可能會覺得有點不舒服,因為“拇指”不可見。 所以請謹慎使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM