How to prevent orientation change for just a layout, not the whole screen/activity

狂风中的少年 提交于 2019-12-23 14:51:34

问题


I would need a child layout (can be any layout, like FrameLayout or RelativeLayout) to ignore orientation change and remain always in landscape orientation. But not its parent or any other sibling layouts/views, they should change their orientation accordingly.

Hence, I cannot use setRequestedOrientation() or android:screenOrientation="landscape". Or it will lock the orientation for the whole screen or the activity. I need to lock only just this single layout.

My layout XML:

RelativeLayout (Parent)
  TextView
    FrameLayout/RelativeLayout <-- this need to be locked
      FrameLayout
        Button

回答1:


It probably depends what you mean by "not change orientation". However, I think that best place to start would be to create your own class for the part that shouldn't change. So the layout xml now has two files:

main_layout.xml

RelativeLayout (Parent)
    TextView
        MyNonChangingLayout

my_non_changing_layout.xml

 RelativeLayout
     FrameLayout
         Button

Where you have created

MyNonChangingLayout extends FrameLayout {
    MyNonchangingLayout(Context content) {
        super(context);
        myContext = context;
        makeFromXML();
    }

private void makeFromXML() {
    LayoutInflater inflater = (LayoutInflater)myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    topView =  inflater.inflate(MyR.layout.my_non_changing_layout, this, false);

    // Get all the sub Views here using topView.findViewById()

    // Do any other initiation of the View you need here

    // Make sure you this otherwise it won't actually appear!
    super.addView(topView);
}

/*
 * Then, you can override quite a lot of the layout's calls and
 * enforce behaviour on the children. Two examples:
 */

// To specifically catch orientation changes
@Overridge
onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // You could create the layout here by removing all views and rebuilding them
    // Perhaps by having a two xml layouts, one of which is "90 degrees out" ...
    // If you do make the layot here, make sure you don't clash with the constructor code!
    switch (newConfig.orientation) {
        case ORIENTATION_LANDSCAPE:
            // Make the layout for this orientation (as per above)
            break;
        case ORIENTATION_PORTRAIT:
            // Make the layout for this orientation (as per above)
            break;
        case ORIENTATION_SQUARE:
            // Make the layout for this orientation (as per above)
            break;
    }
}

//to handle size changes to enforce aspect ratios on children:
@override
protected void onSizeChanged (int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);

    int viewWidth = //something I've determine
    int viewHeight = //something I've determined
    setViewsize(viewToHaveSizedControlled, viewWidth, viewheight);
}

// The post thing means that it doesn't crash no matter which thread it is
// executed on ...
private void setViewsize(final View v, final int w, final int h) {
    post(new Runnable() {
        public void run() {
            ViewGroup.LayoutParams lp = v.getLayoutParams();
            lp.width = w;
            lp.height = h;
            v.setLayoutParams(lp);
    }});
}

}

You can then enforce pretty well anything you want. If you can be more specific about what behaviour you want to enforce on the sub region I might be able to suggest more specific code.

One thing you may be wanting to do is to keep




回答2:


Extend the layout class that you want to keep portrait orientation and override the dispatchConfiurationChanged(Configuration) method as follows.

public void dispatchConfigurationChanged(Configuration newConfig) {
    Configuration c = new Configuration(newConfig); // copy
    c.orientation = ORIENTATION_PORTRAIT; // lock to portrait
    super.dispatchConfigurationChanged(c);
}

Be sure that your application is configured not to restart on orientation changes.



来源:https://stackoverflow.com/questions/14877701/how-to-prevent-orientation-change-for-just-a-layout-not-the-whole-screen-activi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!