限制DreamService的屏幕方向

3
我过去几个小时一直在玩新的DreamService API,它非常棒。不过我有一个问题。
当显示DreamService时,我想将屏幕方向锁定为横屏。 DreamService 的目的是展示许多宽屏照片,而我不想在屏幕上看到黑边。
我尝试了很多方法,包括在Java代码中使用setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)和在清单文件中使用android:screenOrientation="landscape",但都没有生效。
这让我想知道:是否可能锁定DreamService的方向?
3个回答

2
安卓开发团队在不久前的一次直播开发者Hangout中确认,目前无法实现此功能。我相信Google希望DreamServices能够在两种屏幕方向下工作,这就是为什么它不可能的原因。

1

解决方法:使用这个库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>

顺便说一下:允许自动旋转你的Daydream..来自https://code.google.com/p/android/issues/detail?id=40226

"看起来已经实现了这个修复(却没有人告诉我们)。不幸的是,它不能正常工作。在我的Nexus 6p(6.0.1,Build #MHC19Q)上,如果我向左滑动到最左边的屏幕(Google Now?),点击左上角的汉堡菜单,选择设置,然后到底部,有一个“允许主屏幕旋转”的选项。启用该选项后,主屏幕将能够在需要时旋转为横向方向。Daydream随后可以继承该旋转并以横向模式显示。" -- 2016年5月18日


0

很遗憾我也没有找到任何解决方案,我不得不使用一个可能或可能不起作用的恶心的解决方法,即在设置布局之前关闭自动旋转设置:

在你的DreamService类中:

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" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接