![](/img/trans.png)
[英]I am unable to change the text and background color through java code of different layouts in activity?
[英]Change Background of activity dynamically through code
我需要在一些用户活动后更改当前活动的背景
跟随我写的代码
Drawable d = new BitmapDrawable(getResources(),readBitmap(Uri.fromFile(new File(getFilesDir(), imageName))));
this.findViewById(android.R.id.content).setBackground(d);
this.findViewById(android.R.id.content).refreshDrawableState();
public Bitmap readBitmap(Uri selectedImage) {
Bitmap bm = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1; // reduce quality
ParcelFileDescriptor pFileDescriptor = null;
try {
//fileDescriptor = this.getContentResolver().openAssetFileDescriptor(selectedImage, "r");
pFileDescriptor = this.getContentResolver().openFileDescriptor(selectedImage, "r");
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
bm = BitmapFactory.decodeFileDescriptor(pFileDescriptor.getFileDescriptor(), null, options);
pFileDescriptor.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return bm;
}
这是content.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_img_body"
tools:context=".MainActivity" >
另外,如果我删除XML中设置的backgroung,效果很好! 很困惑
修改的XML(有效)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
任何人都可以建议。
谢谢提前
试试this.findViewById(android.R.id.content).setBackgroundDrawable(d);
首先给您的布局ID
第二个findviewbyid布局
第三版layout.setBackgroundResource(getResources()。getDrawable(R.drawable.ready));
问题解决 !!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.