Android 搜索栏 - 设置其可绘制后拇指不可见
我有一个搜索栏,我正在代码中设置可绘制的拇指。当活动开始时,我可以看到拇指的可绘制更改,但是如果我从搜索栏活动启动一个新活动并返回,搜索栏的拇指将变得不可见(仅当我再次将其设置为可绘制时)。仅当我从其他活动返回到搜索栏活动时才会发生这种情况。
我需要在 onRestart() 中更改拇指的可绘制性,因为其他活动可能会更改拇指的颜色或形状,并且我需要刷新它的可绘制性。
我在搜索栏上尝试了 invalidate() 但没有用...
编辑: 我尝试制作 3 个静态 Drawable 对象并在 onCreate() 中加载图像,我注意到返回 SeekBar 活动后,如果我将拇指可绘制对象设置为已设置的对象,则拇指是可见的,但是如果我更改可绘制对象,拇指将变得不可见。
编辑2:
在这种情况下,我将加载的可绘制对象设置为拇指:
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);
}
这是如果我尝试从资源中获取可绘制对象
字符串性别 =获取性别();
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);
在这两种情况下,拇指都变得看不见了。
可能是什么问题?有人知道答案吗? 谢谢你!
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). This is happening only if I come back from other activity to the seekbar activity.
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.
I tried invalidate() on the seekbar but no use...
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.
EDIT 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();
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在使用可绘制对象之前没有设置它的边界。
尝试在 setThumb() 调用之前添加此行:
You are not setting the drawable's bounds before using it.
Try adding this line before the setThumb() call: