简体   繁体   English

Android seekbar-设置其可绘制属性后,拇指不可见

[英]Android seekbar - thumb is invisible after set its drawable

I have a seek bar and I am setting the thumb drawable in code. 我有一个搜索栏,并且正在设置代码中的拇指可绘制。 When the activity is starting I can see the changed drawable for the thumb but if I start a new activity from the seekbar activity and come back, the seekbar's thumb gets invisible (only if I set it's drawable again). 当活动开始时,我可以看到更改后的拇指可绘制对象,但是如果我从seekbar活动中启动了一个新的活动并回来,则该雄键的拇指将变得不可见(仅当我再次将其设置为可绘制对象时)。 This is happening only if I come back from other activity to the seekbar activity. 仅当我从其他活动返回到seekbar活动时,才会发生这种情况。

I need to change the drawable of the thumb in onRestart() because the other activityes may change the color or shape of the thumb and I need to refresh it's drawable. 我需要在onRestart()中更改拇指的可绘制对象,因为其他活动可能会改变拇指的颜色或形状,并且需要刷新它的可绘制对象。

I tried invalidate() on the seekbar but no use... 我在搜索栏上尝试了invalidate()但没有用...

EDIT: I tried to make 3 static Drawable objects and load the images in onCreate() and I noticed that after coming back on the SeekBar activity, if I set the thumb drawable to the one that is already set, the thumb is visible but if I change the drawable, the thumb becomes invisible. 编辑: 我试图制作3个静态Drawable对象并将图像加载到onCreate()中,并且我注意到在返回SeekBar活动之后,如果将拇指可绘制设置为已设置的拇指,则拇指可见,但是如果我更改可绘制对象,拇指变得不可见。

EDIT 2: 编辑2:

In this case I set the loaded drawables to the thumb: 在这种情况下,我将加载的可绘制对象设置为拇指:

String gender = getGender();
    if (gender.equals(Profile.GENDER_1)) {
        mSeekBar.setThumb(mDrawable1);
    } else if (gender.equals(Profile.GENDER_2)) {
        mSeekBar.setThumb(mDrawable2);
    } else {
        mSeekBar.setThumb(mDrawable3);
    }

And this is if I try to get the drawables from the resources 这是如果我尝试从资源中获取可绘制对象

String gender = getGender(); 字符串性别= getGender();

if (gender.equals(Profile.GENDER_1)) {
    mDrawable = mSeekBar.getContext().getResources().getDrawable(R.drawable.slider_thumb_1);
} else if (gender.equals(Profile.GENDER_2)) {
    mDrawable = mSeekBar.getContext().getResources().getDrawable(R.drawable.slider_thumb_2);
} else {
    mDrawable = mSeekBar.getContext().getResources().getDrawable(R.drawable.slider_thumb_3);
}

mSeekBar.setThumb(mDrawable);

In both cases the thumb is getting invisible.. 在这两种情况下,拇指都变得不可见。

What can be the problem? 可能是什么问题? does somebody know the answer? 有人知道答案吗? Thank you! 谢谢!

You are not setting the drawable's bounds before using it. 您在使用可绘制对象之前未设置其范围。

Try adding this line before the setThumb() call: 尝试在setThumb()调用之前添加以下行:

mDrawable.setBounds(0,0, 
    mDrawable.getIntrinsicWidth(), 
    mDrawable.getIntrinsicHeight()
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM