replace layout on orientation change

前端 未结 6 1935
天涯浪人
天涯浪人 2020-12-01 13:03

My app has a webview and some buttons inside a LinerLayout.

the problem is, I want the buttons to be on bottom in portrait mode and on left in landscape mode while t

6条回答
  •  时光说笑
    2020-12-01 13:41

    Make your view a relative layout. When the orientation changes just adjust the layout params for each view in code. Have your buttons be contained in a linear layout

    As in..

    Portrait

    LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    buttonLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
    buttonLinearLayout.setLayoutParams(lp);
    
    
    LayoutParams webParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    lp.addRule(RelativeLayout.ABOVE, R.id.buttons);
    webView.setLayoutParams(webParams);
    

    Landscape

    LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
    lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    buttonLinearLayout.setOrientation(LinearLayout.VERTICAL);
    buttonLinearLayout.setLayoutParams(lp);
    
    
    LayoutParams webParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    lp.addRule(RelativeLayout.TO_RIGHT_OF, R.id.buttons);
    webView.setLayoutParams(webParams);
    

    Make sure the LayoutParams you are using are the RelativeLayout params (always use the Layoutparams of the view's parent)

提交回复
热议问题