【问题标题】:Action Bar Sherlock doesn't keep transparency when orientation is changed from portrait to landscape当方向从纵向更改为横向时,Action Bar Sherlock 不保持透明度
【发布时间】:2012-05-29 19:13:09
【问题描述】:

我在一个展示 VideoView 的项目中使用 Android Sherlock。当屏幕方向为纵向时,会显示视频和一些文本。当是横屏时,只显示全屏视频。

好吧,我的疑问是:为什么当方向为纵向时会显示(Sherlock Action Bar 的)alpha 颜色,而当方向为横向时会改变它的 alpha 属性(变得不透明)?

我的 onCreate() 如下所示:

public void onCreate(Bundle savedInstanceState) {
    // Makes action bar become translucent, and sets layout of this activity.
    requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_video);
}

好吧,我正在使用 requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY),它允许操作栏中的 alpha 颜色。

当方向改变时,我调用 onConfigurationChanged(),它调用 setScreenLayout()。此方法设置视频大小并更改操作栏背景颜色。

/**
 * Sets the screen layout, according with the current orientation.
 * 
 * @param isInLandscapeMode
 */
public void setScreenLayout(Boolean isInLandscapeMode) {
    // If orientation is landscape.
    if (isInLandscapeMode) {
        // Hides text of the layout.
        mTextScrollView.setVisibility(View.GONE);
        // Set the size of the video.
        setVideoDimensions((int)Math.floor(mWindowWidth * (1 / WIDESCREEN_RATIO)), mWindowWidth);
        // Sets window to full screen.
        setWindowToFullScreen();
        // Sets action bar background color.
        getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.color.bg_translucent_black));
    }
    // If orientation is portrait.
    else {
        // Shows text of the layout.
        mTextScrollView.setVisibility(View.VISIBLE);
        // Set the size of the video.
        setVideoDimensions(mWindowWidth, (int)Math.floor(mWindowWidth * WIDESCREEN_RATIO));
        // Sets window to normal screen.
        setWindowToNormalScreen();
        // Sets action bar background color.
        getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.color.bg_translucent_black));
    }
}

这个颜色是在 res/colors.xml 中设置的:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="bg_translucent_black">#11EEEEEE</color>
</resources>

在我的 AndroidManifest.xml 中,我使用 android:configChanges="orientation" 属性设置了 VideoActivity,它允许视频在方向改变时继续播放。

<!-- Video Activity -->
<activity 
    android:name=".VideoActivity" 
    android:label="@string/app_name"
    android:screenOrientation="portrait"
    android:configChanges="orientation"
/>

因此,当方向改变时,不会调用 onCreate()。

我认为正在发生的事情是 requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY) 仅在创建活动时(在本例中为纵向)允许 alpha 在第一个方向。这样,横向不会变成 alpha 颜色。但我不确定。

我确定我需要操作栏在两个方向上的 alpha 颜色。

有人知道我的视频是否可以在两个方向上都有 alpha 颜色吗?

【问题讨论】:

    标签: android


    【解决方案1】:

    我相信 ActionBarSherlock 需要 Android 来处理配置更改,而不是您的应用程序。无法在一个应用中同时使用 android:configChanges “hack”和 ActionBarSherlock。

    【讨论】:

    • 那么,我怎样才能在我的操作栏中为我的视频的两个方向设置两种不同的 alpha 颜色?
    • 在 Activity 的 onCreate 中获取设备的方向,并通过这种方式动态设置 ActionBar 的 alpha 颜色。
    • 我的视频活动的默认方向必须是纵向。所以,我不会得到方向。我会在视频开始后得到它(当可以完成翻转时)。问题是 alpha 不起作用(在播放视频时,每个方向我需要两个不同的 alpha)。 alpha 仅在调用 onCreate() 后才有效。但在这个活动中,我在 AndroidManifest 中使用了 android:configChanges="orientation",它可以防止在方向改变时调用 onCreate()。
    • 我需要与 youtube 应用程序相同的行为(在纵向模式下,操作栏是半透明的,在横向模式下也是半透明的,但全屏)。在 ICS 中,此翻转会暂停视频,然后以新的方向(从停止的位置)重新开始。在我的应用程序中,我不能使用这个进程,因为它必须在 API 的 7 到 10 中运行,它的处理速度较慢。很多时候,这个过程(暂停视频、翻转 cel、重新启动视频)会导致视频延迟。我唯一的选择是在 cel 翻转时更改操作栏 alpha,这不起作用。
    • 要获得与 youtube 应用程序相同的行为,您不能使用 configChanges="orientation"。 ActionBarSherlock 依靠这个来重绘它的视图。要么你拿出 configChanges hack,要么你不使用 ActionBarSherlock。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-10
    • 2011-02-12
    • 2012-07-21
    • 2020-06-12
    • 1970-01-01
    相关资源
    最近更新 更多