自定义半透明Android的动作条 [英] Custom Translucent Android ActionBar

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

问题描述

我已经淘到interwebs(如Android的文档,这里的答案,等)的答案,我认为将是一个相当琐碎的问题。你如何实现像在谷歌音乐和半透明操作栏< A HREF =htt​​p://coreylatislaw.com/wp-content/uploads/2011/07/youtube.png> YouTube的(链接图像的例子)?

我要的视频内容是全屏,而不是受限/向下推操作杆,同时仍然利用一个内置的UI组件的好处。我可以明显地使用一个完全自定义的看法,但我如果可能的话宁愿利用动作条。

清单

 &LT;活动
    机器人:名称=录像机
    机器人:主题=@安卓风格/ Theme.Holo
    机器人:launchMode =singleTop
    机器人:configChanges =keyboardHidden |定位/&GT;
 

活动

  //设置操作栏
动作条动作条= getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
查看customActionBarView = getLayoutInflater()膨胀(R.layout.player_custom_action_bar,空)。
actionBar.setCustomView(customActionBarView,
                        新ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT,
                                                   R.dimen.action_bar_height));
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
 

菜单

 &LT;菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android&GT;
    &LT;项目
        机器人:ID =@ + ID /按钮1
        机器人:图标=@可绘制/按钮1
        机器人:showAsAction =总是/&GT;

    &LT;项目
        机器人:ID =@ + ID /按钮2
        机器人:图标=@可绘制/按钮2
        机器人:showAsAction =总是/&GT;
&LT; /菜单&gt;
 

自定义动作条查看

 &LT;的LinearLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / custom_action_bar
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:方向=横向
    机器人:重力=中心
    机器人:背景=@色/ TranslucentBlack&GT;

    &LT;! - 用箭头主页图标 - &GT;
    &LT; ImageView的
        机器人:ID =@ + ID /家
        机器人:SRC =@可绘制/家
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =match_parent/&GT;

    &LT;! - 另一幅图像 - &GT;
    &LT; ImageView的
        机器人:ID =@ + ID /图像
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =match_parent
        机器人:能见度=看见/&GT;

    &LT;! - 文字,如果没有图像 - &GT;
    &LT;的TextView
        机器人:ID =@ + ID /文
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =match_parent
        机器人:重力=center_vertical
        机器人:能见度=水涨船高/&GT;

    &LT;! - 标题 - &GT;
    &LT;的TextView
        机器人:ID =@ + ID /标题
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =match_parent
        机器人:以下属性来=20dp
        机器人:重力=center_vertical/&GT;
&LT; / LinearLayout中&GT;
 

解决方案

如果你希望你的活动将是全屏,但仍然表现出动作条,但有一个字母,你有权要求overlaymode的动作条中的的onCreate()的活动:

  getWindow()requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY)。

//getWindow().requestFeature(WindowCompat.FEATURE_ACTION_BAR_OVERLAY); &LT;&LT;使用此API为7+(V7支持库)
 

你叫的setContentView(..)(因为的setContentView(..)还初始化动作条旁边设置内容),你可以在你的动作条设置背景绘制:

<$p$p><$c$c>getActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.actionbar_bg));

这可以是一个形状绘制与投入资源的α/绘制:

 &LT; XML版本=1.0编码=UTF-8&GT?;
&LT;形状的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android机器人:形状=矩形&GT;
    [固体机器人:颜色=#BB000000/&GT;
&LT; /形状&GT;
 

您也可以通过建立这样做完全编程一个 Col​​orDrawable

  getActionBar()setBackgroundDrawable(新ColorDrawable(Color.argb(128,0,0,0)))。
 

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

  @android:款式/ Theme.NoTitleBar.Fullscreen
 

或编程调用

  getActionBar()隐藏()。
 

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

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.

Manifest

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

Activity

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

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

Custom ActionBar View

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

解决方案

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) 

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

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>

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

or programmatically by calling

getActionBar().hide();

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆