这是我的申请的简短草图
...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start);
... //at this place, getWindow().setBackgroundDrawableResource(int resid)
//would work perfectly
mainView();
}
void mainView() {
setContentView(R.layout.main);
...
if (...) {
getWindow().setBackgroundDrawableResource(R.drawable.anyDrawable);
}
...
}
但是在mainView()中,该方法不会产生任何影响。并没有抛出异常。
main.xml已经定义了一个背景图像,但是start.xml没有这样做。这会导致我的问题吗?
或者我可以用另一种方式更改背景图像吗?
PS你不能再设置一次内容视图,你应该做的是在布局中有父视图,其ID如main,然后使用
findViewById(R.id.main).setBackgroundDrawableResource(R.drawable.anyDrawable);
在xml布局中,您需要设置要设置的视图的ID(这应该是最顶层的视图)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dip"
android:id="@+id/main"></LinearLayout>
将id添加到视图后,需要保存布局并构建项目
如果你想发布你的xml代码以及你的布局名称,我可以为你写更多