[英]How to change the background image
I need to change the backgound image of the same activity multiple times, I'd like to know how can I do that using codes, since XML won't help in this case (I guess) 我需要多次更改同一活动的背景图像,我想知道如何使用代码来做到这一点,因为在这种情况下XML不能提供帮助(我想)
here is the code of my activity, it's simple and there are few lines, if someone could change this code to explain better how i can do what i need, i'd be very thankfull. 这是我的活动代码,很简单,几乎没有几行,如果有人可以更改此代码以更好地解释我该如何做我需要做的事情,我将非常感激。 thank you guys.
感谢大伙们。
public class forca_inicia extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forca_inicia);
Editable palavra_jogo;
EditText palavra = (EditText)findViewById(R.id.campo_palavra);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_forca_inicia, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
In onCreate
get the root element from your activity layout and set color, drawable, resource. 在
onCreate
,从活动布局中获取根元素,并设置颜色,可绘制资源。 For example: 例如:
FrameLayout frame = (FrameLayout) findViewById(R.id.content_frame);
frame.setBackground(Drawable background);
frame.setBackgroundResource(int resId);
frame.setBackgroundColor(int color);
I would also recommend you to follow Java naming conventions. 我还建议您遵循Java命名约定。 Class names should start with capital letter and should use CamelCase eg: ForcaInicia.
类名应以大写字母开头,并应使用CamelCase,例如:ForcaInicia。 And variables should start with small letter and use camelCase eg palavraJogo.
并且变量应以小写字母开头,并使用camelCase,例如palavraJogo。
您需要做的就是访问“活动”的ViewGroup并设置该ViewGroup的背景。
getWindow().getDecorView().setBackgroundResource(R.drawable.your_drawable);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.