activity 背景在 android 工作室
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);
}
});
试试这个
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));
这不是你做事的方式。 findViewById()
仅在当前 Activity 视图中搜索,在您的代码中是 R.layout.settings
。
使用
startActivityForResult(new Intent(MainActivity.this,Settings.class), 1);
开始设置。
在设置中activity添加
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();
}
});
在你的主 activity 添加
@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 );
}
更多信息:How to manage `startActivityForResult` on Android?
我在 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);
}
});
试试这个
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));
这不是你做事的方式。 findViewById()
仅在当前 Activity 视图中搜索,在您的代码中是 R.layout.settings
。
使用
startActivityForResult(new Intent(MainActivity.this,Settings.class), 1);
开始设置。
在设置中activity添加
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();
}
});
在你的主 activity 添加
@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 );
}
更多信息:How to manage `startActivityForResult` on Android?