如何动态设置相对布局背景? 我的相对布局在顶部,不能设置id
答案 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));