首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何以编程方式设置背景图像?

如何以编程方式设置背景图像?
EN

Stack Overflow用户
提问于 2014-11-11 22:16:27
回答 2查看 12.2K关注 0票数 3

我在以编程方式设置背景时遇到了问题。我做不到。

这是我的主题课。代码中有单选按钮,可以更改主背景。到目前为止,我已经为两个单选按钮(即radioButtonMountains和radioButtonSea)编写了实现,它们应该加载两个不同的图像(即mountains.png和sea.png)。

代码语言:javascript
运行
复制
public class ThemeActivity extends BasicActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_theme);

        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setTitle("Motywy");

        setAction();
    }

    private void setAction(){

        relativeLayout=(RelativeLayout) findViewById(R.id.relativeLayoutid);

        radioGroup=(RadioGroup) findViewById(R.id.radioGroup);
        radioButtonMountains=(RadioButton) findViewById(R.id.radioMountains);
        radioButtonCity=(RadioButton) findViewById(R.id.radioCity);
        radioButtonSea=(RadioButton) findViewById(R.id.radioSea);
        radioButtonNature=(RadioButton) findViewById(R.id.radioNature);

        radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                if(radioButtonMountains.isChecked()){

                    //relativeLayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.mountains));
                    //relativeLayout.setBackground(getResources().getDrawable(R.drawable.mountains));
                    relativeLayout.setBackgroundResource(R.drawable.mountains);                 
            }
            else if(radioButtonCity.isChecked()){

            }
            else if(radioButtonSea.isChecked()){
                relativeLayout.setBackgroundResource(R.drawable.sea);
            }
            else if(radioButtonNature.isChecked()){

            }

        }
    });             
}

这是我的主类的activity_main.xml (如果你想的话,我可以用我的主类粘贴代码)。我添加了‘android:id=’@+id/relativeLayoutid“,上面提到了它。

代码语言:javascript
运行
复制
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayoutid" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/sea"
    android:orientation="vertical" >

// other things like a ImageButton, a TextView etc

</RelativeLayout>

最后,当我单击其中一个单选按钮时,将被替换为:

代码语言:javascript
运行
复制
11-11 21:28:34.172: D/AndroidRuntime(22316): Shutting down VM
11-11 21:28:34.172: W/dalvikvm(22316): threadid=1: thread exiting with uncaught exception (group=0x411162a0)
11-11 21:28:34.202: E/AndroidRuntime(22316): FATAL EXCEPTION: main
11-11 21:28:34.202: E/AndroidRuntime(22316): java.lang.NullPointerException
11-11 21:28:34.202: E/AndroidRuntime(22316):    at com.example.runapp.ThemeActivity$1.onCheckedChanged(ThemeActivity.java:43)
11-11 21:28:34.202: E/AndroidRuntime(22316):    at android.widget.RadioGroup.setCheckedId(RadioGroup.java:174)
11-11 21:28:34.202: E/AndroidRuntime(22316):    at android.widget.RadioGroup.access$600(RadioGroup.java:54)
11-11 21:28:34.202: E/AndroidRuntime(22316):    at android.widget.RadioGroup$CheckedStateTracker.onCheckedChanged(RadioGroup.java:358)
11-11 21:28:34.202: E/AndroidRuntime(22316):    at android.widget.CompoundButton.setChecked(CompoundButton.java:140)
11-11 21:28:34.202: E/AndroidRuntime(22316):    at android.widget.CompoundButton.toggle(CompoundButton.java:92)
11-11 21:28:34.202: E/AndroidRuntime(22316):    at android.widget.RadioButton.toggle(RadioButton.java:76)
11-11 21:28:34.202: E/AndroidRuntime(22316):    at android.widget.CompoundButton.performClick(CompoundButton.java:104)
11-11 21:28:34.202: E/AndroidRuntime(22316):    at android.view.View$PerformClick.run(View.java:17082)
11-11 21:28:34.202: E/AndroidRuntime(22316):    at android.os.Handler.handleCallback(Handler.java:615)
11-11 21:28:34.202: E/AndroidRuntime(22316):    at android.os.Handler.dispatchMessage(Handler.java:92)
11-11 21:28:34.202: E/AndroidRuntime(22316):    at android.os.Looper.loop(Looper.java:137)
11-11 21:28:34.202: E/AndroidRuntime(22316):    at android.app.ActivityThread.main(ActivityThread.java:4867)
11-11 21:28:34.202: E/AndroidRuntime(22316):    at java.lang.reflect.Method.invokeNative(Native Method)
11-11 21:28:34.202: E/AndroidRuntime(22316):    at java.lang.reflect.Method.invoke(Method.java:511)
11-11 21:28:34.202: E/AndroidRuntime(22316):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
11-11 21:28:34.202: E/AndroidRuntime(22316):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
11-11 21:28:34.202: E/AndroidRuntime(22316):    at dalvik.system.NativeStart.main(Native Method)

应用程序就关闭了。我试过这样做:

代码语言:javascript
运行
复制
relativeLayout.setBackgroundResource(R.drawable.mountains);

这样:

代码语言:javascript
运行
复制
relativeLayout.setBackground(getResources().getDrawable(R.drawable.mountains));

这样:

代码语言:javascript
运行
复制
relativeLayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.mountains));

但它们都不起作用。

我怎样才能解决这个问题?我该纠正什么?

解决方案:

  1. 将relativeLayout=(RelativeLayout) findViewById(R.id.relativeLayoutid)和setBackgroundResource()方法移到setContentView()方法后面的主要活动。
  2. 创建一个静态变量,并将这个变量放在setBackgroundResource()方法中作为参数。
  3. 在有单选按钮的主题活动中更改静态变量。

MainActivity类:

代码语言:javascript
运行
复制
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public class MainActivity extends BasicActivity implements OnClickListener {

    public final static String EXTRA_MESSAGE = "com.example.helloworld.MESSAGE";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //other things

        setContentView(R.layout.activity_main);

        relativeLayout=(RelativeLayout) findViewById(R.id.relativeLayoutid);

        if(image==0) relativeLayout.setBackgroundResource(R.drawable.mountains);
        else relativeLayout.setBackgroundResource(image);

        //other things
    }

    //other things

    public RelativeLayout relativeLayout;
    public static int image;
}

activity_main.xml for MainActivity类:

代码语言:javascript
运行
复制
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayoutid" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/sea"
    android:orientation="vertical" >

// other things like a ImageButton, a TextView etc

</RelativeLayout>

更改主要背景的ThemeActivity类:

代码语言:javascript
运行
复制
public class ThemeActivity extends BasicActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_theme);

        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setTitle("Theme");

        setAction();
    }

    private void setAction(){

        radioGroup=(RadioGroup) findViewById(R.id.radioGroup);
        radioButtonMountains=(RadioButton) findViewById(R.id.radioMountains);
        radioButtonCity=(RadioButton) findViewById(R.id.radioCity);
        radioButtonSea=(RadioButton) findViewById(R.id.radioSea);
        radioButtonNature=(RadioButton) findViewById(R.id.radioNature);

        setRadioChoose();

        radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                if(radioButtonMountains.isChecked()){

                    MainActivity.image=R.drawable.mountains;
                }
                else if(radioButtonCity.isChecked()){

                    MainActivity.image=R.drawable.city;
                }
                else if(radioButtonSea.isChecked()){

                    MainActivity.image=R.drawable.sea;                  
                }
                else if(radioButtonNature.isChecked()){

                    MainActivity.image=R.drawable.nature;   
                }

                saveRadioChoose(checkedId);
            }
        });

    }

    private void setRadioChoose(){

        radioChoose=PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        radioChooseEdit=radioChoose.edit();

        int radio=radioChoose.getInt("RADIO", 0);

        if(radio==radioButtonMountains.getId()) radioButtonMountains.setChecked(true);
        else if(radio==radioButtonCity.getId()) radioButtonCity.setChecked(true);
        else if(radio==radioButtonSea.getId()) radioButtonSea.setChecked(true);
        else if(radio==radioButtonNature.getId()) radioButtonNature.setChecked(true);
        else if(radio==0) radioButtonMountains.setChecked(true);
    }

    private void saveRadioChoose(int checkedId){

        radioChooseEdit.putInt("RADIO", checkedId);
        radioChooseEdit.commit();
    }

    private RadioGroup radioGroup;
    private RadioButton radioButtonMountains;
    private RadioButton radioButtonCity;
    private RadioButton radioButtonSea;
    private RadioButton radioButtonNature;

    private SharedPreferences radioChoose;
    private SharedPreferences.Editor radioChooseEdit;
}
EN

回答 2

Stack Overflow用户

发布于 2014-11-11 22:42:21

我在购物车//目录应用程序中使用了这段代码。所述无线电按钮用于保存所述产品颜色信息,因此选择以具有不同颜色背景的无线电按钮表示的颜色的用户确定该图像的产品颜色。

代码语言:javascript
运行
复制
 color[i] = new RadioButton(getActivity());
 color[i].setButtonDrawable(R.drawable.color_radio_button);
 if (sdkVersion < 16) {
    color[i].setBackgroundDrawable(bkg);
 } else {
    color[i].setBackground(bkg);
 }

可拉

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/color_radio_selected" android:state_focused="true"/>
<item android:drawable="@drawable/color_radio_selected" android:state_pressed="true"/>
<item android:drawable="@drawable/color_radio_selected" android:state_selected="true"/>
<item android:drawable="@drawable/color_radio_selected" android:state_checked="true"/>
<item android:drawable="@drawable/color_radio_unselected"/>

</selector>

图像

票数 1
EN

Stack Overflow用户

发布于 2017-08-24 08:18:51

imageView.setBackgroundResource(R.drawable.bgg);RelativeLayout imageView = (RelativeLayout) mLockscreenView.findViewById(R.id.mainImage)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26875635

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档