2014-12-15 3 views
0

동일한 액티비티의 backgound 이미지를 여러 번 변경해야합니다. XML이이 경우 도움이되지 않을 수 있으므로 코드를 사용하여 어떻게 할 수 있는지 알고 싶습니다.배경 이미지를 변경하는 방법

여기 내 활동의 코드입니다. 간단하고 줄이 적습니다. 누군가 내가이 코드를 바꿔서 내가 필요한 것을 어떻게 할 수 있는지를 설명 할 수 있다면, 나는 매우 감사 할 것입니다. 감사합니다.

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); 
} 
} 

답변

1

onCreate 활성 레이아웃에서 루트 요소를 가져 와서 색상, 드로어 블, 리소스를 설정하십시오. 예 :

FrameLayout frame = (FrameLayout) findViewById(R.id.content_frame); 

frame.setBackground(Drawable background); 
frame.setBackgroundResource(int resId); 
frame.setBackgroundColor(int color); 

Java 명명 규칙을 따르는 것이 좋습니다. 수업 이름은 대문자로 시작해야하며 예 : ForcaInicia와 같은 CamelCase를 사용해야합니다. 변수는 작은 글자로 시작하고 camelCase를 사용합니다. palavraJogo.

0

활동의 ViewGroup에 액세스하고 해당 ViewGroup의 배경을 설정하기 만하면됩니다.

getWindow().getDecorView().setBackgroundResource(R.drawable.your_drawable); 
+0

일해 주셔서 감사합니다. –

관련 문제