How to change spinner background image?

2019-08-29 07:32发布

问题:

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);
      }
}