0

我创建了一个球形视频播放器,并希望防止它包含的视图旋转(包含 3D 场景的 Unity 视图的 Framelayout。)我希望控件旋转(它们存在于容器内的相对布局中)。但我不希望统一容器旋转。这可能吗?

这是布局(所有控件都在布局中):

<FrameLayout
            android:id="@+id/unity_layout"
            android:visibility="invisible"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

        <FrameLayout
                android:id="@+id/unity_container"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
        </FrameLayout>

        <include layout="@layout/player_controls"/>

</FrameLayout>

图片: https ://dl.dropboxusercontent.com/u/17087393/pivotalscreens/Screenshot_2015-07-19-23-36-31.jpg

4

1 回答 1

0

我可以建议您采用以下解决方案,

步骤 1.创建两个不同的布局 ( layout& layout_land) 并定义设计

步骤 2.使用扩展的自定义视图FrameLayout()

步骤 3. 将常规 FrameLayout 替换为您的自定义视图,

<MyNonChangingLayout
 android:id="@+id/unity_container"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content">
</MyNonChangingLayout>

MyNonChangingLayout() 的示例,

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);
        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);
        }});
    }

这个答案最初是在这里给出的,

如何防止仅针对布局而不是整个屏幕/活动的方向更改

于 2015-07-20T06:04:34.683 回答