查看了View中含有setBackgroundResource(int id)。但是怎么获得主窗口的view。(不是imageview)
怎么得到一个更换activity中的背景图片的方法。android:background可以设置,但是只能设置一次。我想做一个android换肤的函数,每次点击一个按钮触发onClick,里面就是设置更换的背景图片的函数。但是现在遇到了问题就是在onClick中无法获得Activity的View,这也就无法使用view.setBackgroundResource(int id)。希望高手给个解答,在线等结果。
有人说了用getWindow(),但是我试过不好使,希望大家给一个好用的方法。我把我的函数写出来了:
skinButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
Log.i("skinButton", "onClick invoked.");
???.setBackgroundDrawable(R.drawable.bg1);

}catch(Exception e){
Log.e(VideoTransmission.tag, "Fail to Change Skin" + e.getMessage());
e.printStackTrace();
}
}

});
    }加粗部分就是现在还不知道怎么获取view的方法,谢谢大家

解决方案 »

  1.   

    我不晓得一般是怎么做的,但是至少可以用两种方法,一种是用一个全屏的ImageView来当作背景,通过修改imageview来修改背景图片,一种是将你xml中最外层的那个布局,LinerLayout之类的,添加id属性,在程序中通过这个来setbackgroundResource。
      

  2.   

    第一种方法我无法实现,因为我这个activity中已经有一个ImageView了,如果再设置一个就会覆盖我的原来的布局。第二种方法我正在试,等会给你答复~
      

  3.   

    悲剧呀,LinearLayout无法添加id属性,最外层的布局就是imageview(我这个是小尺寸的,不能满屏显示),看样子两种方法都行不通呀
      

  4.   

    xml文件是怎么写的,贴上来看看
      

  5.   

    最外层加个LinearLayout 添加个id属性 就行了啊
      

  6.   

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/bg1" android:oneshot="false"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:gravity="center_horizontal">
    <TextView
    android:text="Welcome!"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:textColor="@color/welcomecolor"
    android:textStyle="bold"
    android:textSize="25dp"
    />
    <ImageView android:id="@+id/imageview1"
    android:adjustViewBounds="true" android:maxHeight="200dp"
    android:maxWidth="200dp" android:scaleType="fitXY"
    android:layout_width="wrap_content" android:layout_height="wrap_content">
    </ImageView>
    <TextView
    android:text="Please choose what you want to do below:"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:shadowColor="@color/shadowcolor"
    android:shadowRadius="3.0"
    android:paddingTop="20dp"
    android:lines="2"
    />
    <TableLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:stretchColumns="*"
    >
    <TableRow>
    <Button
    android:text="视频播放"
    android:textColor="@color/buttoncolor"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button1"></Button>
    <Button
    android:text="视频采集"
    android:textColor="@color/buttoncolor"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button2"></Button>
    </TableRow>
    <TableRow>
    <Button
    android:text="欢迎动画"
    android:textColor="@color/buttoncolor"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button3"></Button>
    <Button
    android:text="换肤"
    android:textColor="@color/buttoncolor"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button4"></Button>
    </TableRow>
    </TableLayout>
    </LinearLayout>
      

  7.   

    最外层的LinearLayout不能添加属性呀,是我的方法不对吗,为什么我添加id属性就报错呢?
      

  8.   

    我添加了没问题啊。<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/bg"
        >
    而且,我直接用        HelloWorld.this.getWindow().setBackgroundDrawableResource(R.drawable.icon);getWindow也可以实现,前面的HelloWorld是当前Activity的名称
      

  9.   

    我再试试看,我用view.getparent().setBackgroundResource(R.drawable.icon)发现屏幕图片只改了最下面部分,上面没改,不知道为什么
      

  10.   

    终于调通了,谢谢这个好方法,虽然view.getparent().setBackgroundResource(R.drawable.icon)不好用,但是添加id是好使的。感谢
      

  11.   

    我再问一下,怎么能够通过view.setBackgroundResource(R.drawable.#);更改多张图片。
    我的#部分是多张drawable,点击一次按钮换取一下背景图片,请问有什么办法吗?
      

  12.   

    我再问一下,怎么能够通过view.setBackgroundResource(R.drawable.#);更改多张图片。
    我的#部分是多张drawable,点击一次按钮换取一下背景图片,请问有什么办法吗?
      

  13.   

    这个你要设置一个flag,int型或者枚举型,用来标识你当前是哪个背景,每次点击都按照一定的顺序切换背景并且更换flag
      

  14.   

    你是不是在同一个activity上更换啊,如果是的话,应该是没有问题的。。
    另想请问下高手,不同activity上的更换背景咋弄
      

  15.   

    可以啊,你在布局.xml里面设置了linerlayout的id属性,然后去到maiactivity里面用findviewbyid,把对应的id get出来,然后用setBackgroundResource(R.drawable.图片),图片为你想要的id,就可以了。
      

  16.   

    怎样在一个activity中对多个activity的背景进行设置?
      

  17.   

    只能是linerlayout的吗??..可不可以是其它的布局,如AbsoluteLayout,呢。
      

  18.   

    其他布局都一样吧,只要给布局id,在activity那获取一下,就能使用setBackgroundResource(R.drawable.#);