我在android中询问了关于ui更新的问题。(How to change imageview background color in a preference in android?)
它适用于Android 4.1.2版本。我在2.3.3,3.0和4.3安卓版本中测试它,我发现它在这些版本中不起作用。为了简化问题,我想问一下为什么下面的代码不适用于这些版本:
public class SettingsIncomingSearchFragment extends PreferenceListFragment
implements SharedPreferences.OnSharedPreferenceChangeListener
{
@Override
public void onCreate(Bundle icicle)
{
<!-- other codes -->
setBackgroundColor() ;
<!-- other codes -->
}
private void setBackgroundColor() {
// TODO Auto-generated method stub
LinearLayout mainlayout ;
LinearLayout sublayout ;
View view ;
ImageView img ;
SharedPreferences sp ;
sp = ctx.getSharedPreferences(SHARED_PREFS_NAME, ctx.MODE_PRIVATE);
LayoutInflater inflater = (LayoutInflater)
getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.rectangle_layout, null);
mainlayout = (LinearLayout)view.findViewById(R.id.rectangle_main_layout_ll);
sublayout = (LinearLayout)mainlayout.findViewById(R.id.rectangle_layout_ll);
String defValue = null ;
defValue = sp.getString("incomingsearchbackgroundcolor", String.valueOf(Color.BLACK));
if(defValue != null)
{
img = (ImageView)sublayout.findViewById(R.id.iv_priority);
img.setBackgroundColor(Integer.parseInt(defValue));
}
}
谢谢!