本文介绍了自定义半透明 Android ActionBar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在搜索互联网(例如 Android 文档、此处的答案等),以寻找我认为相当微不足道的问题的答案.你如何实现像 Google MusicYouTube(链接是图片示例)?

I've been scouring the interwebs (e.g. Android documentation, answers here, etc.) for the answer to what I thought would be a fairly trivial question. How do you achieve a translucent action bar like the ones in Google Music and YouTube (links are image examples)?

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

I want video content to be full screen and not constrained/pushed down by the action bar while still leveraging the benefits of a built in UI component. I can obviously use a completely custom view, but I'd rather leverage the ActionBar if possible.

清单

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

自定义操作栏视图

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

推荐答案

如果你想让你的活动全屏但仍然显示一个动作栏,但是你必须在 onCreate() 中为动作栏请求覆盖模式 你的活动:

If you want your activity to be fullscreen but still show an actionbar, but with an alpha you have to request overlaymode for the actionbar in onCreate() of your activity:

getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);

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

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

Then after you call setContentView(..) (since setContentView(..) also initializes the actionbar next to setting the content) you can set a background drawable on your actionbar:

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

可以是在 res/drawable 中放入 alpha 的可绘制形状:

which can be a shape drawable with an alpha put in res/drawable:

<?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>

您也可以通过创建 ColorDrawable 以完全编程的方式完成此操作:

You could also do this completely programatically by creating a ColorDrawable:

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

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

Otherwise ofcourse you can hide the actionbar completely; in that case you can set a custom theme on your activity in your manifest:

@android:style/Theme.NoTitleBar.Fullscreen

或以编程方式调用

getActionBar().hide();

这篇关于自定义半透明 Android ActionBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 23:47