当前位置:编程学习 > wap >>

怎么实现在一个Activity里面更换背景

比如点一下Button,背景图片更换 Activity Android --------------------编程问答-------------------- 给你的主layout 就是main.xml 最外层的layout 加个ID 然后获取它,然后setbackground 之类的 --------------------编程问答-------------------- image1.setBackgroundResource(R.drawable.imageName); --------------------编程问答--------------------
//这里的parent就是主layout (默认也就是main.xml)最外层的layout 加的那个id 
View parentView = findViewById(R.id.parent);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            parentView.setBackgroundResource(R.drawable.imageName);
                        }
                    });
--------------------编程问答-------------------- 楼上正解,当然也可以使用样式,统一该 --------------------编程问答-------------------- 注意的是,使用了样式,需要重新onCreate才能看到效果 --------------------编程问答-------------------- 3楼正解。。。 --------------------编程问答-------------------- setBackgroundResource(R.drawable.imageName); --------------------编程问答-------------------- 这样都麻烦啊,有选择器的啊
<?xml version="1.0"?>
<!--
  Copyright (C) 2010 The Android Open Source Project

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
 -->

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/btn_speak_pressed" />
    <item android:state_selected="true" android:drawable="@drawable/btn_speak_selected" />
    <item android:drawable="@drawable/btn_speak_normal" />
</selector> --------------------编程问答-------------------- 其实更换背景图片的方法有不少,setBackground,setDrawable等等
补充:移动开发 ,  Android
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,