2015-09-24 40 views
0

我需要知道如何在一個活動上隨機設置背景, 'android:background =「@ drawable/backG」'只顯示一個圖片 背景隨機隨機發射應用程序不斷顯示,直到用戶退出應用程序,當他重新打開它時,它會顯示一個新的背景 注意:我只有一個活動在我的項目 在此先感謝如何在一個活動中隨機設置多個背景

回答

0

我認爲,你應該使用Java代碼來做到這一點。
例如,你有RelativeLayout的是在你的活動主要佈局
您應該使用

RelativeLayout rLayout = (RelativeLayout) findViewById (R.id.rLayout); 
Resources res = getResources(); //resource handle 
Drawable drawable = res.getDrawable(R.drawable.newImage); //new Image that was added to the res folder 
rLayout.setBackground(drawable); 

對於隨機背景:您創建一個包含所有你圖片的背景 一個數組,並使用下面的代碼隨機位置

Random randomGenerator = new Random(); 
int randomPositionInt = randomGenerator.nextInt(10); // it will generate position in 0 - 10 

希望這有助於

+0

非常感謝我很感激 –

相關問題