728x90

출처 : 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

'안드로이드 > Layout' 카테고리의 다른 글

Meterial Design 로그인 Layout 예제  (0) 2020.03.21
ScrollView  (0) 2019.12.20
Android Fragment 기본 예제  (0) 2018.09.11
FloatingActionButton(FAB)  (0) 2018.08.15
LinearLayout weight  (0) 2018.03.01
블로그 이미지

Link2Me

,