我一直在网上搜索(例如Android文档,这里的答案等),寻找我认为是相当微不足道的问题的答案。如何实现像Google Music和YouTube那样的半透明操作栏(链接是图像示例)?
我希望视频内容是全屏的,不受操作栏的限制/下推,同时仍然利用内置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>
如果你想让你的活动是全屏的,但仍然显示一个动作栏,但有一个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(..)
也在设置内容的旁边初始化动作栏),你可以在你的动作栏上设置一个可绘制的背景:
getActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.actionbar_bg));
,它可以是一个可绘制的形状,alpha值放在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
:
getActionBar().setBackgroundDrawable(new ColorDrawable(Color.argb(128, 0, 0, 0)));
否则你当然可以完全隐藏动作栏;在这种情况下,你可以在manifest中为activity设置自定义主题:
@android:style/Theme.NoTitleBar.Fullscreen
或通过编程方式调用
getActionBar().hide();
除了正确的答案,如果你正在使用AppcomatActivity,调用supportRequestWindowFeature而不是
旧版本:getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
你想使用:
@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);
它将使用覆盖的ActionBar
而不是按下你的窗口
上面的代码对于操作栏是绝对正确的。
不是getActionBar().setBackgroundDrawable(new ColorDrawable(Color.argb(128, 0, 0, 0)))
使用getActionBar().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
通过这种方式,您将能够完全透明地看到操作栏。
我是这样做的:
1)通过调用
以编程方式请求操作栏叠加 getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
这个'requestFeature()'必须在调用'setContentView(r.l outout .my_layout)'之前从你的活动'onCreate()'方法请求:
2)通过调用setBackgroundDrawable()
来设置动作栏的颜色,如下所示:
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00212121")) );
这个方法需要一个drawable的引用,或者你也可以使用颜色解析器来解析十六进制颜色值(前两位数字用于alpha通道,从#00到#FF)
请注意,我使用的是actionBarSherlok getSupportActionBar()
,但这应该适用于任何操作栏。
如果你仍然不能让这个工作,尝试添加到你的主题样式
<item name="windowActionBarOverlay">true</item>
<item name="android:actionBarStyle">@style/ActionBar.Transparent.Example</item>
<item name="android:windowActionBarOverlay">true</item>
我想和大家分享一下我实现这个目标的步骤:
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表示完全透明
第一步:首先,在style .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);
就是这样,你会得到透明Actionbar的结果。同样,这里如果你想显示抽屉切换按钮和菜单,它会显示它们