如何动态设置相对布局背景图像?

时间:2012-07-18 16:53:04

标签: android android-layout

如何动态设置相对布局背景? 我的相对布局在顶部,不能设置id

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

尝试使用Xamarin.Android(跨平台)

RelativeLayout relativeLayout = new RelativeLayout(this);

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

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