Android屏幕旋转不生效
Posted
技术标签:
【中文标题】Android屏幕旋转不生效【英文标题】:Android screen rotation not taking effect 【发布时间】:2021-02-28 11:05:43 【问题描述】:我可以检查如何通过 android 中的代码旋转屏幕吗? 我正在使用的设备(Android 6 ARM CPU)没有传感器来检测方向变化,我需要手动进行。 这个设备的问题是它不会保存设置,一旦我通过设置菜单更改屏幕方向。 下次开机时,它会回到默认的“风景”。 我尝试使用以下代码(一次一个)来更改它,它不起作用(意味着屏幕没有变化)。
Settings.System.putInt(contentResolver, Settings.System.USER_ROTATION, 1);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
在此设备上,有两个选项(屏幕旋转和外部屏幕旋转)。 我需要通过设置 - 显示选项更改两个方向以实现我想要的。 Kotlin 或 Java 代码对我来说都可以。有什么建议吗?
【问题讨论】:
【参考方案1】:您不能在应用程序中旋转 android 核心,但您可以在项目中创建自定义布局并由您自己控制,如下所示:
首先创建自定义布局:
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;
import android.widget.FrameLayout;
public class URotateLayout extends FrameLayout
public static final String TAG = "URotateLayout";
public static final int ORIENTATION_PORTRAIT = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
public static final int ORIENTATION_LANDSCAPE = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
public static final int ORIENTATION_SENSOR_LANDSCAPE = ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE;
public static final int ORIENTATION_SENSOR_PORTRAIT = ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT;
public static final int ORIENTATION_SENSOR = ActivityInfo.SCREEN_ORIENTATION_SENSOR;
public static final int ORIENTATION_LOCKED = ActivityInfo.SCREEN_ORIENTATION_LOCKED;
private int mOrientation;
private int mLastOrientation;
private int mDefaultVideoContainerWidth;
private int mDefaultVideoContainerHeight;
private int mScreenWidth;
private int mScreenHeight;
public URotateLayout(Context context)
super(context);
public URotateLayout(Context context, AttributeSet attrs)
super(context, attrs);
public URotateLayout(Context context, AttributeSet attrs, int defStyleAttr)
super(context, attrs, defStyleAttr);
public void updateScreenWidthAndHeight()
Display display = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
mScreenWidth = metrics.widthPixels;
mScreenHeight = metrics.heightPixels;
public boolean isLandscape()
updateScreenWidthAndHeight();
return mScreenWidth > mScreenHeight;
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
updateScreenWidthAndHeight();
if (isLandscape())
mDefaultVideoContainerWidth = mScreenWidth;
mDefaultVideoContainerHeight = mScreenHeight;
else
mDefaultVideoContainerWidth = mScreenWidth;
// mDefaultVideoContainerHeight = mScreenWidth * 9 / 16;
mDefaultVideoContainerHeight=mScreenHeight;
setMeasuredDimension(mDefaultVideoContainerWidth, mDefaultVideoContainerHeight);
public int getOrientation()
return mOrientation;
public void setOrientation(int orientation)
if (getContext() instanceof Activity)
Activity mActivity = (Activity) getContext();
switch (orientation)
case ORIENTATION_PORTRAIT:
mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
case ORIENTATION_LANDSCAPE:
mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
case ORIENTATION_SENSOR_LANDSCAPE:
mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
break;
case ORIENTATION_SENSOR_PORTRAIT:
mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
break;
case ORIENTATION_SENSOR:
mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
break;
case ORIENTATION_LOCKED:
mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
break;
mOrientation = orientation;
invalidate();
public void locked()
mLastOrientation = mOrientation;
setOrientation(ORIENTATION_LOCKED);
public boolean isLocked()
return mOrientation == ORIENTATION_LOCKED ? true : false;
public void unlocked()
setOrientation(mLastOrientation);
public void toggleOrientation()
if (getContext() instanceof Activity && mOrientation != ORIENTATION_LOCKED)
Activity mActivity = (Activity) getContext();
if (isLandscape())
mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
else
mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
if (mOrientation == ORIENTATION_SENSOR)
getHandler().postDelayed(new Runnable()
@Override
public void run()
setOrientation(mOrientation);
, 2000);
第二次将它添加到你的布局中,像这样的 xml 文件:
<yourPackage_name.URotateLayout
android:id="@+id/rotate_layout"
android:layout_
android:layout_
android:layout_gravity="center">
<your_layout_here/>
</yourPackage_name.URotateLayout>
最后像这样在你的活动或片段中使用它:
//define the view i use the **butterknife**
@Bind(R.id.rotate_layout)
URotateLayout rotateLayout;
@Override
public void toggleScreenOrientation()
if (rotateLayout != null)
rotateLayout.toggleOrientation();
//use this for set oriention
public void setScreenOriention(int oriention)
rotateLayout.setOrientation(oriention);
【讨论】:
这可能对其他人有帮助。我会保持原样。我不能使用这种方法的原因是:用户想要旋转整个屏幕(不仅是我的应用程序,整个系统屏幕)。他们不想通过设置菜单中的详细信息来更改它。以上是关于Android屏幕旋转不生效的主要内容,如果未能解决你的问题,请参考以下文章