Setting background colour of Android layout element

后端 未结 9 1936
北海茫月
北海茫月 2020-12-02 03:42

I am trying to, somewhat clone the design of an activity from a set of slides on Android UI design. However I am having a problem with a very simple task.

I have cre

9条回答
  •  广开言路
    2020-12-02 04:32

    The answers above all are static. I thought I would provide a dynamic answer. The two files that will need to be in sync are the relative foo.xml with the layout and activity_bar.java which corresponds to the Java class corresponding to this R.layout.foo.

    In foo.xml set an id for the entire layout:

    
    
    

    And in activity_bar.java set the color in the onCreate():

    public class activity_bar extends AppCompatActivty {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.foo);
    
                //Set an id to the layout
            RelativeLayout currentLayout = 
                        (RelativeLayout) findViewById(R.id.foo);
    
            currentLayout.setBackgroundColor(Color.RED);
            ...
        }
        ...
    }
    

    I hope this helps.

提交回复
热议问题