How to change spinner background image?

蹲街弑〆低调 提交于 2019-12-11 22:30:48

问题


Hi i am new to android,I am using spinner background image. The spinner contains four values, and each one has images.If i click the spinner values, the images will be changed in the spinner background. Please help me how can i change the background image when the spinner value is changed?


回答1:


Give Id to the parent (Linear or relative) layout of your main.xml file and in the java file,if the four values of your spinner is Mercury,Venus,Earth,Mars, then in the Onselected itemlistener,put code like this

public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
      Toast.makeText(parent.getContext(), "The planet is " +
          parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
      if(parent.getItemAtPosition(pos).toString().equalsIgnoreCase("Mercury"))
      {
          ll1.setBackgroundResource(R.drawable.java_j2me);
      }
      if(parent.getItemAtPosition(pos).toString().equalsIgnoreCase("Venus"))
      {
          ll1.setBackgroundResource(R.drawable.image);
      }
      if(parent.getItemAtPosition(pos).toString().equalsIgnoreCase("Earth"))
      {
          ll1.setBackgroundResource(R.drawable.javaandroid);
      }
      if(parent.getItemAtPosition(pos).toString().equalsIgnoreCase("Mars"))
      {
          ll1.setBackgroundResource(R.drawable.party);
      }
}


来源:https://stackoverflow.com/questions/5829295/how-to-change-spinner-background-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!