自定义半透明Android ActionBar

问题描述 投票:52回答:6

我一直在搜索互联网(例如Android文档,这里的答案等),以寻找我认为是一个相当微不足道的问题的答案。你如何实现像Google MusicYouTube中的半透明动作栏(链接是图像示例)?

我希望视频内容是全屏的,不受操作栏的约束/推送,同时仍然利用内置UI组件的优势。我显然可以使用完全自定义的视图,但如果可能的话,我宁愿利用ActionBar。

表现

<activity
    android:name="videoplayer"
    android:theme="@android:style/Theme.Holo"
    android:launchMode="singleTop"
    android:configChanges="keyboardHidden|orientation"/>

活动

// Setup action bar
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
View customActionBarView = getLayoutInflater().inflate(R.layout.player_custom_action_bar, null);
actionBar.setCustomView(customActionBarView,
                        new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT,
                                                   R.dimen.action_bar_height));
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

菜单

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/button1"
        android:icon="@drawable/button1"
        android:showAsAction="always"/>

    <item
        android:id="@+id/button2"
        android:icon="@drawable/button2"
        android:showAsAction="always"/>
</menu>

自定义ActionBar视图

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/custom_action_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center"
    android:background="@color/TranslucentBlack">

    <!--Home icon with arrow-->
    <ImageView
        android:id="@+id/home"
        android:src="@drawable/home"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"/>

    <!--Another image-->
    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:visibility="visible"/>

    <!--Text if no image-->
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:visibility="gone"/>

    <!--Title-->
    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:paddingLeft="20dp"
        android:gravity="center_vertical"/>
</LinearLayout>
android android-actionbar android-5.0-lollipop android-4.0-ice-cream-sandwich android-3.0-honeycomb
6个回答
101
投票

如果您希望自己的活动是全屏但仍然显示操作栏,但是使用alpha,您必须为活动的onCreate()中的操作栏请求overlaymode:

getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);

//getWindow().requestFeature(WindowCompat.FEATURE_ACTION_BAR_OVERLAY); << Use this for API 7+ (v7 support library) 

然后在调用setContentView(..)之后(因为setContentView(..)也会在设置内容旁边初始化操作栏),您可以在操作栏上设置背景可绘制:

getActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.actionbar_bg));

这可以是一个可绘制的形状,带有放入res / drawable的alpha:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
    <solid android:color="#BB000000" /> 
</shape>

您也可以通过创建qazxsw poi以编程方式完成此操作:

ColorDrawable

否则你可以完全隐藏动作栏;在这种情况下,您可以在清单中为活动设置自定义主题:

getActionBar().setBackgroundDrawable(new ColorDrawable(Color.argb(128, 0, 0, 0)));

或通过调用以编程方式

@android:style/Theme.NoTitleBar.Fullscreen

12
投票

除了正确的答案,如果您使用的是AppcomatActivity,请调用supportRequestWindowFeature而不是

旧版本:getActionBar().hide();

你想用:

getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);

9
投票

我认为你应该能够使用Window标志@Override protected void onCreate(Bundle savedInstanceState) { supportRequestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY); super.onCreate(savedInstanceState); } 来做到这一点。

在您的活动中致电FEATURE_ACTION_BAR_OVERLAY之前,

呼叫:

setContentView()

并且它将使用覆盖的getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY); 而不是按下你的窗口。


6
投票

上面的代码对于制作操作栏是绝对正确的。

代替

ActionBar

使用

getActionBar().setBackgroundDrawable(new ColorDrawable(Color.argb(128, 0, 0, 0)))

通过这种方式,您将能够看到操作栏完全透明。


5
投票

以下是我如何使用它:

1)通过调用以编程方式请求操作栏覆盖

getActionBar().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

在调用'setContentView(R.layout.my_layout)'之前,必须从您的活动'onCreate()'方法请求'requestFeature()':

2)通过调用 getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY); 设置动作栏颜色,如下所示:

setBackgroundDrawable()

此方法需要对drawable的引用,或者您也可以使用颜色解析器来解析十六进制颜色值(前两个数字用于从#00到#FF开始的alpha通道)

请注意,我正在使用actionBarSherlok getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00212121")) ); ,但这应该适用于任何操作栏。

如果您仍然无法使其工作,请尝试将其添加到您的主题样式

getSupportActionBar()

0
投票

我只想与您分享我为实现这一目标所采取的步骤:

1)在您的活动的OnCreate,

<item name="windowActionBarOverlay">true</item>
 <item name="android:actionBarStyle">@style/ActionBar.Transparent.Example</item>
 <item name="android:windowActionBarOverlay">true</item>

然后

getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);

这是在setContentView之前完成的。

2)在setContentView之后,您可以执行以下操作

getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
            WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

其中alpha值为0表示它是完全透明的。

© www.soinside.com 2019 - 2024. All rights reserved.