안드로이드/Layout

Dynamic Layouts

Link2Me 2019. 1. 6. 11:53

출처 : https://stackoverflow.com/questions/17070047/how-to-set-layout-dynamically-in-android


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    LayoutParams default_layout_params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    View view1 = inflater.inflate(R.layout.layout1, null);
    addContentView(view1, default_layout_params);          
    View view2 = inflater.inflate(R.layout.layout2, null);
    addContentView(view2, default_layout_params);
    view2.setVisibility(View.INVISIBLE);
    view1.setVisibility(View.VISIBLE);
    view1.bringToFront();
} 


private View view1, view2;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    view1 = getLayoutInflater().inflate(R.layout.layout1, null);
    view2 = getLayoutInflater().inflate(R.layout.layout2, null);
    setContentView(view1);

    someBtn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            setContentView(view2);
        }
    });


    someBtn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            setContentView(view1);
        }
    });
}


728x90