pen*_*uru 30 android android-widget android-layout
我正在使用一个drawable for seekbar thumb with
android:thumb="@drawable/thumb"
Run Code Online (Sandbox Code Playgroud)
如何在倾角单元中设置此拇指的大小?因为我使用了类似于搜索栏的样式
<style name="CustomSeekBar">
<item name="android:indeterminateOnly">false</item>
<item name="android:minHeight">8dip</item>
<item name="android:maxHeight">8dip</item>
<item name="android:thumbOffset">8dip</item>
</style>
Run Code Online (Sandbox Code Playgroud)
我想要拇指的12dip
高度和宽度.
and*_*rew 43
为我设置拇指大小最灵活的方法是创建带有形状的分层绘图作为占位符thumb_image.xml
:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape>
<size
android:height="40dp"
android:width="40dp" />
<solid android:color="@android:color/transparent" />
</shape>
</item>
<item android:drawable="@drawable/scrubber_control_normal_holo"/>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
因此,基本上改变形状的大小将拉伸拉伸,形状是透明的,因此它是不可见的.此拇指的最小尺寸也是位图的大小.
这是布局的例子:
<SeekBar
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:thumb="@drawable/thumb_image" />
<SeekBar
android:id="@+id/seekBar2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Run Code Online (Sandbox Code Playgroud)
Poo*_*oks 12
我也在寻找一种方法来做类似的事情,在查看了其他一些问题并将所有答案放在一起后,我想出了一种方法来调整onCreate()中的Seekbar拇指.
这是我的代码onCreate()
,略微适应您的需求.
ViewTreeObserver vto = mySeekBar.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
Resources res = getResources();
Drawable thumb = res.getDrawable(R.drawable.thumb);
int h = mySeekBar.getMeasuredHeight() * 1.5; // 8 * 1.5 = 12
int w = h;
Bitmap bmpOrg = ((BitmapDrawable)thumb).getBitmap();
Bitmap bmpScaled = Bitmap.createScaledBitmap(bmpOrg, w, h, true);
Drawable newThumb = new BitmapDrawable(res, bmpScaled);
newThumb.setBounds(0, 0, newThumb.getIntrinsicWidth(), newThumb.getIntrinsicHeight());
mySeekBar.setThumb(newThumb);
mySeekBar.getViewTreeObserver().removeOnPreDrawListener(this);
return true;
}
});
Run Code Online (Sandbox Code Playgroud)
请注意,这适用于调整拇指大小,但可能会被剪裁,因为它比包含的搜索栏大(不确定).如果它被剪裁,您可能需要创建一个更高的搜索栏并创建自己的搜索栏背景,以匹配您想要显示的尺寸.
希望这可以帮助!
从sdk你可以看到这是基本SeekBar的风格:
<style name="Widget.SeekBar">
<item name="android:indeterminateOnly">false</item>
<item name="android:progressDrawable">@android:drawable/progress_horizontal</item>
<item name="android:indeterminateDrawable">@android:drawable/progress_horizontal</item>
<item name="android:minHeight">20dip</item>
<item name="android:maxHeight">20dip</item>
<item name="android:thumb">@android:drawable/seek_thumb</item>
<item name="android:thumbOffset">8dip</item>
<item name="android:focusable">true</item>
</style>
Run Code Online (Sandbox Code Playgroud)
用这个作为拇指选择器:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:state_window_focused="true"
android:drawable="@drawable/seek_thumb_pressed" />
<item android:state_focused="true"
android:state_window_focused="true"
android:drawable="@drawable/seek_thumb_selected" />
<item android:state_selected="true"
android:state_window_focused="true"
android:drawable="@drawable/seek_thumb_selected" />
<item android:drawable="@drawable/seek_thumb_normal" />
</selector>
Run Code Online (Sandbox Code Playgroud)
您应该能够使用它们作为基础来替换当前正在使用的drawable(这看起来就像您已经到过的阶段).
基于可绘制文件夹大小的桶(等)drawable-hdpi
,这些drawable可以在不同的屏幕上具有不同的尺寸,drawable-large-hdpi
或者您可以使不同SeekBar
的样式/可绘制的尺寸看起来不同:
<style name="SeekBar.Thumb.Smiley" parent="@android:style/Widget.SeekBar">
<item name="android:thumb">@drawable/smiley</item>
<style>
<style name="SeekBar.Thumb.Smiley.Large" parent="@android:style/Widget.SeekBar">
<item name="android:thumb">@drawable/smiley_large</item>
<style>
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/SeekBar.Thumb.Smiley.Large"/>
Run Code Online (Sandbox Code Playgroud)