在onResume中,我获得了需要更改其可见性的窗口小部件的引用。日志猫显示,首选项中的设置正确返回(show_images,show_titles,show_descriptions)。日志猫显示if else应当按原样放入每个if else语句中。但是在设备上,大约有50%的时间在启动时某些卡会显示为空白。在进行偏好更改后,有些将完全显得空白,而有些时候它似乎可以正常工作。我尝试从设备进行clean,cleanbuild和CleanUnInstall,但没有运气。这是在onResume中。在调用父onResume并获得偏好之后。
Log.i("INFO", show_images + "" + " "+ show_titles +" " + show_descriptions );
//force the layout to load before changing properties
try{
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
if(!show_images){
Log.i("INFO", "show images false");
image1.setVisibility(View.GONE);
image2.setVisibility(View.GONE);
image3.setVisibility(View.GONE);
image4.setVisibility(View.GONE);
image5.setVisibility(View.GONE);
} else{
Log.i("INFO", "show images true");
image1.setVisibility(View.VISIBLE);
image2.setVisibility(View.VISIBLE);
image3.setVisibility(View.VISIBLE);
image4.setVisibility(View.VISIBLE);
image5.setVisibility(View.VISIBLE);
}
if(!show_titles){
Log.i("INFO", "show titles false");
rTitle1.setVisibility(View.GONE);
rTitle2.setVisibility(View.GONE);
rTitle3.setVisibility(View.GONE);
rTitle4.setVisibility(View.GONE);
rTitle5.setVisibility(View.GONE);
}else{
Log.i("INFO", "show titles true");
rTitle1.setVisibility(View.VISIBLE);
rTitle2.setVisibility(View.VISIBLE);
rTitle3.setVisibility(View.VISIBLE);
rTitle4.setVisibility(View.VISIBLE);
rTitle5.setVisibility(View.VISIBLE);
}
if(!show_descriptions){
Log.i("INFO", "show desc false");
rDescription1.setVisibility(View.GONE);
rDescription2.setVisibility(View.GONE);
rDescription3.setVisibility(View.GONE);
rDescription4.setVisibility(View.GONE);
rDescription5.setVisibility(View.GONE);
}else{
Log.i("INFO", "show desc true");
rDescription1.setVisibility(View.VISIBLE);
rDescription2.setVisibility(View.VISIBLE);
rDescription3.setVisibility(View.VISIBLE);
rDescription4.setVisibility(View.VISIBLE);
rDescription5.setVisibility(View.VISIBLE);
}
}`
不确定为什么。但是将布局从constraintLayout转换为linearlayouts。然后相同的onResume可以控制小部件的可见性。需要对布局问题进行调整,但是数据持久性的工作原理是