[英]activity backgrounds in android studio
我在Android Studio中有问题。
我尝试从另一个Activity更改Activity背景,但是当我运行该应用程序时,它不起作用并且该应用程序关闭
public class Settings extends Activity
{
RelativeLayout rl ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
RadioButton st2 = (RadioButton)findViewById(R.id.style2);
final SeekBar sk = (SeekBar)findViewById(R.id.seekBar);
st2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
rl = (RelativeLayout) findViewById(R.id.mainActivity);
rl.setBackgroundResource(R.drawable.sh4);
}
});
这不是您的方式。 findViewById()
仅在当前“活动”视图中搜索,在您的代码中为R.layout.settings
。
采用
startActivityForResult(new Intent(MainActivity.this,Settings.class), 1);
开始设置。
在设置活动中添加
st2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Intent intent = new Intent();
intent.putExtra("background_res", R.drawable.sh4);
setResult(RESULT_OK, intent);
finish();
}
});
在您的主要活动中添加
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data == null) {return;}
int res = data.getIntExtra("background_res");
rl = (RelativeLayout) findViewById(R.id.mainActivity);
rl.setBackgroundResource(res );
}
尝试这个
View nameofyourview = findViewById(R.id.randomViewInMainLayout);
// Find the root view
View root = nameofyourview.getRootView()
// Set the color
root.setBackgroundColor(getResources().getColor(android.R.color.red));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.