2012-07-18 74 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));