col*_*bug 53 android android-3.0-honeycomb android-4.0-ice-cream-sandwich android-actionbar android-5.0-lollipop
我一直在搜索互联网(例如Android文档,这里的答案等),以获得我认为是一个相当微不足道的问题的答案.您如何实现像Google音乐和YouTube中的半透明操作栏(链接是图像示例)?
我希望视频内容是全屏的,不受操作栏的约束/推送,同时仍然利用内置UI组件的优势.我显然可以使用完全自定义的视图,但如果可能的话,我宁愿利用ActionBar.
表现
<activity
android:name="videoplayer"
android:theme="@android:style/Theme.Holo"
android:launchMode="singleTop"
android:configChanges="keyboardHidden|orientation"/>
Run Code Online (Sandbox Code Playgroud)
活动
// 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);
Run Code Online (Sandbox Code Playgroud)
菜单
<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>
Run Code Online (Sandbox Code Playgroud)
自定义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>
Run Code Online (Sandbox Code Playgroud)
MrJ*_*Jre 102
如果您希望自己的活动是全屏但仍然显示操作栏,但是使用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)
Run Code Online (Sandbox Code Playgroud)
然后在你打电话后setContentView(..)(因为setContentView(..)也在设置内容旁边初始化动作栏)你可以在你的动作栏上设置一个背景画面:
getActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.actionbar_bg));
Run Code Online (Sandbox Code Playgroud)
这可以是一个可绘制的形状,带有放入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>
Run Code Online (Sandbox Code Playgroud)
你也可以通过创建一个完全编程的方式完成ColorDrawable:
getActionBar().setBackgroundDrawable(new ColorDrawable(Color.argb(128, 0, 0, 0)));
Run Code Online (Sandbox Code Playgroud)
否则你可以完全隐藏动作栏; 在这种情况下,您可以在清单中为活动设置自定义主题:
@android:style/Theme.NoTitleBar.Fullscreen
Run Code Online (Sandbox Code Playgroud)
或通过调用以编程方式
getActionBar().hide();
Run Code Online (Sandbox Code Playgroud)
yon*_*nCN 12
除了正确的答案,如果您使用的是AppcomatActivity,请调用supportRequestWindowFeature而不是
旧版:
getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
你想用:
@Override
protected void onCreate(Bundle savedInstanceState) {
supportRequestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
super.onCreate(savedInstanceState);
}
Run Code Online (Sandbox Code Playgroud)
我认为你应该可以使用Window标志来做到这一点FEATURE_ACTION_BAR_OVERLAY.
在你打电话之前setContentView(),
呼叫:
getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
Run Code Online (Sandbox Code Playgroud)
它会使用叠加ActionBar而不是按下你的窗口.
上面的代码对于制作操作栏是绝对正确的.
代替
getActionBar().setBackgroundDrawable(new ColorDrawable(Color.argb(128, 0, 0, 0)))
Run Code Online (Sandbox Code Playgroud)
使用
getActionBar().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
Run Code Online (Sandbox Code Playgroud)
通过这种方式,您将能够看到操作栏完全透明.
小智 5
以下是我如何使用它:
1)通过调用以编程方式请求操作栏覆盖
getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
Run Code Online (Sandbox Code Playgroud)
在调用'setContentView(R.layout.my_layout)'之前,必须从您的活动'onCreate()'方法请求'requestFeature()':
2)通过调用setBackgroundDrawable()如下设置动作栏颜色:
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00212121")) );
Run Code Online (Sandbox Code Playgroud)
此方法需要对drawable的引用,或者您也可以使用颜色解析器来解析十六进制颜色值(前两个数字用于从#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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
52428 次 |
| 最近记录: |