[英]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" />
編輯:好的,經過一些測試和嘗試一些東西后,我了解到:
android:thumb="@null"
將刪除底部和頂部填充現在我需要知道是否有更簡潔的方法來移除填充物,以及所有設備上的拇指大小是否相同? 歡迎任何幫助。
要從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
可以解決自定義的拇指可繪制對象被填充為空白的問題。 步驟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:minHeight和android: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.