2012-07-18 75 views
2

如何動態設置相對佈局背景? 我在頂部的相對佈局,不能設置ID如何動態設置相對佈局背景圖片?

+0

我不確定這是什麼意思:「我在頂部的相對佈局,不能設置ID」,但與Raghav Sood的解決方案,您可以使用'<?xml version =「1.0」encoding =「utf- 8「?> ' – Sam 2012-07-18 16:59:50

回答

10

假設你有一個ID爲relativeLayout的相對佈局。您可以通過設置背景:

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

rLayout.setBackgroundDrawable(drawable); 
+0

'getDrawable()'和'setBackgroundDrawable()'已棄用 – Mac 2015-06-09 19:04:11

0

嘗試此Xamarin.Android(跨平臺)

的RelativeLayout的RelativeLayout =新的RelativeLayout(本);

RelativeLayout relativeLayout =(RelativeLayout)FindViewById(Resource.Id.relativeLayout);

relativeLayout.SetBackgroundDrawable(Resources.GetDrawable(Resource.Drawable.imageName));