【问题标题】:Custom Translucent Android ActionBar自定义半透明Android ActionBar
【发布时间】:2011-07-19 14:56:57
【问题描述】:

我一直在搜索互联网(例如 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>

自定义操作栏视图

<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


【解决方案1】:

如果您希望您的活动全屏但仍显示操作栏,但带有 alpha 您必须在活动的onCreate() 中为操作栏请求叠加模式:

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)你可以在你的actionbar上设置一个背景drawable:

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>

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

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

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

@android:style/Theme.NoTitleBar.Fullscreen

或以编程方式调用

getActionBar().hide();

【讨论】:

  • 谢谢!我认为这是我缺少的一些简单的东西。
  • 你知道如何调整操作栏的高度吗?我的维度好像没有效果! actionBar.setCustomView(customActionBarView, new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, R.dimen.action_bar_height));
  • 我不确定您是否真的可以更改操作栏的高度,我猜不能,但如果可能的话,我建议您不要这样做,因为它是蜂窝中的权威 UI 组件。
  • 有没有办法在同一个活动中切换覆盖与实体操作栏?即使用 viewSwitcher 从地图视图覆盖操作栏切换到列表视图实体操作栏(因此列表项不会呈现在操作栏下方)...
  • @tote,您只需使用纯色背景致电getActionBar().setBackgroundDrawable(...) 即可进行更改。
【解决方案2】:

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

旧版本: getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);

你想使用:

@Override
protected void onCreate(Bundle savedInstanceState) {
    supportRequestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
    super.onCreate(savedInstanceState);
}

【讨论】:

    【解决方案3】:

    我认为您应该能够使用 Window 标志 FEATURE_ACTION_BAR_OVERLAY 来做到这一点。

    在活动中调用setContentView()之前,

    致电:

    getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
    

    它会使用覆盖的ActionBar,而不是按下你的窗口。

    【讨论】:

      【解决方案4】:

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

      代替

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

      使用

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

      这样,您将能够看到完全透明的操作栏。

      【讨论】:

        【解决方案5】:

        我是这样工作的:

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

               getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
        

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

        2) 像这样调用setBackgroundDrawable() 设置操作栏颜色:

        getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00212121")) );
        

        此方法需要对可绘制对象的引用,或者您也可以使用颜色解析器来解析十六进制颜色值(前 2 位用于从 #00 到 #FF 的 alpha 通道)

        请注意,我使用的是 actionBarSherlok getSupportActionBar(),但这应该适用于任何操作栏。

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

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

        【讨论】:

          【解决方案6】:

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

          1) 在您的活动的 OnCreate 中,

          getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
          

          然后

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

          这是在 setContentView 之前完成的。

          2) setContentView 之后,可以进行如下操作

           getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.argb(0, 100, 181, 246)));
          

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

          【讨论】:

            【解决方案7】:

            第一步: 首先,在styles.xml中设置你的主题

                <style name="AppTheme.NoActionBar">
                    <item name="windowActionBar">false</item>
                    <item name="windowNoTitle">true</item>
                </style>
            

            第二步: 其次,您必须在 AndroidManifest.xml 中为您的活动设置主题

                <activity android:name=".activity.YourActivity"
                    android:theme="@style/AppTheme.NoActionBar"/>
            

            第三步: 现在,在您的活动布局文件中设置如下:

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/agri_bg_login">
                    <android.support.v7.widget.Toolbar
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/tool_bar"
                        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
                </RelativeLayout>
            

            最后,您可以在活动中设置它。

                Toolbar tool_bar = findViewById(R.id.tool_bar);
                setSupportActionbar(tool_bar);
                getSupportActionbar().setTitle("Your actionbar title");
                getSupportActionbar().setDisplayHomeAsUpEnabled(true);
            

            就是这样,您将获得透明操作栏的结果。另外,这里如果你想显示抽屉切换按钮和菜单,它会显示它们。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-12-25
              • 2011-03-01
              • 2020-10-26
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多