0

Например у меня есть две картинки в drawable как сделать чтобы при нажатии на button мог меняться фон Layout (а)?

1
  • "т е фон всего приложения, ну или экрана" - простите, но что тут написано? 10 окт 2019 в 14:44

1 ответ 1

0

Через массив можно менять:

private int[] images;  // declare your array in global scope
private int imagesIndex = 0;
Then you need to populate your Array with Drawable Resources. You can do it in onCreate() method(if using Array in `Activity).

int numOfImages = 4;
images = new int[numOfImages];
images[0] = R.drawable.ic_launcher_background;
images[1] = R.drawable.ic_launcher_foreground;
images[2] = R.drawable.fiona;
images[3] = R.drawable.shrek;
Finally in Click Listener of your Button you simply need to select a resource from your Array.

 @Override
 public void onClick(View v) {
     LinearLayout view = (LinearLayout) findViewById(R.id.layout1);
     view.setBackgroundResource(images[imagesIndex]); 
     ++imagesIndex;  // update index, so that next time it points to next resource
     if (imagesIndex == images.length - 1)
         imagesIndex = 0; // if we have reached at last index of array, simply restart from beginning.
  }

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.