如何更改特定布局的方向从纵向到横向,如android中的Framelayout。

时间:2018-02-01 03:35:13

标签: android android-layout android-studio

我构建了一个显示我的youtube chanel的应用。我遇到的问题是当用户按下youtube客户端上的全屏按钮时,将方向更改为横向。我希望当用户在youtube上选择全屏按钮时,屏幕会自动切换到横向视图,当用户从landscapr视图中调出全屏按钮时,方向将变为纵向,但状态保持不变。整个屏幕不能再刷新。这是我用于全屏的代码示例,但它只显示全屏而不是将屏幕转换为横向。

    @Override
    public void onShowCustomView(View view, CustomViewCallback callback) {
        // if a view already exists then immediately terminate the new one
        if (mCustomView != null) {
            callback.onCustomViewHidden();
            return;
        }
        mContentView = (DrawerLayout) findViewById(R.id.drawer_layout);
        mContentView.setVisibility(View.GONE);
        mCustomViewContainer = new FrameLayout(MainActivity.this);
        mCustomViewContainer.setLayoutParams(LayoutParameters);
        mCustomViewContainer.setBackgroundResource(android.R.color.black);
        view.setLayoutParams(LayoutParameters);
        mCustomViewContainer.addView(view);
        mCustomView = view;
        mCustomViewCallback = callback;
        mCustomViewContainer.setVisibility(View.VISIBLE);
        mCustomViewContainer.setSystemUiVisibility();
        setContentView(mCustomViewContainer);
    }

    @Override
    public void onHideCustomView() {
        if (mCustomView == null) {
            return;
        } else {
            // Hide the custom view.
            // Hide the custom view.
            mCustomView.setVisibility(View.GONE);
            // Remove the custom view from its container.
            mCustomViewContainer.removeView(mCustomView);
            mCustomView = null;
            mCustomViewContainer.setVisibility(View.GONE);
            mCustomViewCallback.onCustomViewHidden();
           /* ViewGroup parent = (ViewGroup) mContentView.getParent();
            if (parent != null) {
                parent.removeAllViews();
            }*/
            // Show the content view.
            mCustomViewContainer=null;
            mContentView.setVisibility(View.VISIBLE);

            setContentView(mContentView);

        }
    }

1 个答案:

答案 0 :(得分:0)

在所有活动中添加清单 - android:screenOrientation="portrait"

<activity android:name=".MainActivity" android:label="@string/app_name" android:screenOrientation="portrait">

您可以将此用于横向

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

要更改为纵向模式,请使用

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

编辑: 如果你想旋转视图或可绘制

rotateAnimationLayout.xml

<rotate  xmlns:android="http://schemas.android.com/apk/res/android"
       android:fromDegrees="0" 
       android:toDegrees="-90"
       android:pivotX="50%"
       android:duration="0"
       android:fillAfter="true" />

活动或片段中的代码

    RotateAnimation rotate= (RotateAnimation)AnimationUtils.loadAnimation(this,R.anim.rotateAnimation);
  yourView.setAnimation(rotate);