Setting background image in java

后端 未结 1 736
刺人心
刺人心 2021-01-13 17:48

So basically, I have an xml, which i want to reuse. The content is somewhat the same, only the background is different and a few adjustment too.. How can I go about to do th

1条回答
  •  遥遥无期
    2021-01-13 18:06

    You can try to assign an id for your layout on your xml so that on your java code, you can set a different layout for it.. Here is an example:

     //assuming your Layout is named linearlayout1:
     LinearLayout ll = (LinearLayout) findViewById(R.id.linearlayout1);
     ll.setBackgroundResource(R.drawable.sample);
    

    You can also create if statements before setting your background like:

    LinearLayout ll = (LinearLayout) findViewById(R.id.linearlayout1);
    if( yourifstatement) {
    ll.setBackgroundResource(R.drawable.sample);
    }
    

    If it is a RelativeLayout, then the same code applies, just change LinearLayout to RelativeLayout.

    If this is not the problem, please post your LogCat.

    0 讨论(0)
提交回复
热议问题