DreamService 的锁屏方向



在过去的几个小时里,我一直在摆弄新的DreamService API,它非常甜蜜。不过,我确实有一个问题。

我想在显示DreamService时将屏幕方向锁定为横向。DreamService的目的是显示一些宽屏照片,我宁愿屏幕顶部和底部没有黑条。

我已经尝试了很多东西,包括 Java 代码中的setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)和清单文件中的android:screenOrientation="landscape",但没有一个奏效。

这让我想知道:是否可以锁定DreamService的方向?

Android 开发团队确认,在前段时间的实时开发者环聊中,这目前是不可能的。

我相信谷歌希望DreamServices在两个屏幕方向上工作,所以这就是为什么这是不可能的。

解决方法:使用此库 https://github.com/rongi/rotate-layout

build.gradle

 dependencies {
        compile 'rongi.rotate-layout:rotate-layout:2.0.0'
    }

your_daydream_layout.xml

<com.github.rongi.rotate_layout.layout.RotateLayout  
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   app:angle="-90">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <WebView
        android:id="@+id/daydream_webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#2a2e31" />
    <TextView
        android:id="@+id/dream_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="sans-serif-light"
        android:text="..."
            android:textColor="#abc"
            android:textSize="20sp" />
    </RelativeLayout>
</com.github.rongi.rotate_layout.layout.RotateLayout>

顺便说一句:允许自动旋转你的白日梦..从 https://code.google.com/p/android/issues/detail?id=40226

"看起来已经实施了解决此问题(没有人告诉 我们)。 不幸的是,它无法正常工作。 在我的 Nexus 6p (6.0.1, Build #MHC19Q),如果我滑动到最左侧的屏幕(Google Now?),点击 左上角的汉堡菜单,选择设置,然后转到 底部,有一个主屏幕选项的"允许旋转"。 跟 启用后,主屏幕将能够旋转为横向 需要时定向。 然后白日梦可以继承这种旋转和 也以横向模式显示" -- 2016年5月18日

很遗憾,我也没有找到任何解决方案,我不得不使用一种讨厌的解决方法,它可能对您有用,也可能不适合您,它是在设置布局之前设置自动旋转设置:

在您的梦想服务课程中:

import android.provider.Settings;
<...>
    @Override
public void onAttachedToWindow() {
    Settings.System.putInt(getContentResolver(),
            android.provider.Settings.System.ACCELEROMETER_ROTATION, 0);
    setContentView(R.layout.dream_main);
<...>

在清单中:

<uses-permission android:name="android.permission.WRITE_SETTINGS" />