透明动作栏和优步等状态栏 [英] Transparent action bar and status bar like Uber
问题描述
我已经对这个主题做了一些研究,但是我找不到一些针对我的App / Activity的解决方案。
I've done a few researches on this topic but I couldn't found some solution for my App/ Activity.
- 我试图获得一个透明的操作栏。
首先,我尝试更改AndroidManifest.xml中的主题:
First, I have tried to change the themes in my AndroidManifest.xml:
values / styles.xml:
<style name="AppTheme.ActionBar.Transparent" parent="AppTheme">
<item name="android:windowContentOverlay">@null</item>
<item name="windowActionBarOverlay">true</item>
<item name="colorPrimary">@android:color/transparent</item>
和
值 - v21 / styles.xml:
<style name="AppTheme.ActionBar.Transparent" parent="AppTheme">
<item name="colorPrimary">@android:color/transparent</item>
</style>
但结果如下:
< img src =https://i.stack.imgur.com/05ySe.pngalt =在此处输入图像说明>
所以我只是得到了一些亮点灰色动作条左侧/右侧有一些间隙。
So I only get some light grey action bar with some gaps on the left/ right side.
其次,我尝试过使用 fitsSystemWindow =true
和 android:background =我的布局中有@android:color / transparent
,但它也没有解决问题。
Second, I have tried using fitsSystemWindow="true"
and android:background="@android:color/transparent"
in my layouts, but it also didn't fix the issue.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.poweranimal.letsgo.Application.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"/>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
但我得到的输出与以前相同:( 2.另外我也试过了透明状态栏:
But I get the same output as before:( 2. In addition I have tried also the get a transparent status bar with:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
和
<item name="android:statusBarColor">@android:color/transparent</item>
但这没有也没有工作(没有改变)。
but this didn't work too (nothing has changed).
我真正想要的是这样的:
What I actually want is something like this:
< img src =https://i.stack.imgur.com/7qg4b.pngalt =在此输入图像说明>
如果有人有,那会很酷对我来说可能是一个解决方案。
Would be cool if anyone has a possible solution for me.
提前致谢。
推荐答案
1。首先,您必须更改布局
结构,以在透明<下显示 MAP
code>工具栏& StatusBar
还要删除从您现有的工具栏
中左右 gap
。
1. First, you have to change your layout
structure to show MAP
under transparent Toolbar
& StatusBar
and also to remove left and right gap
from your existing Toolbar
.
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_marginTop="24dp"
android:elevation="1dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/sample_map">
<!-- put your content here -->
</LinearLayout>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
2。在 MainActivity $中c $ c>,在下面做一些事情来初始化
工具栏
并使工具栏
和 StatusBar
as transparent
。
2. In your MainActivity
, do below things to initialize Toolbar
and make Toolbar
and StatusBar
as transparent
.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// Toolbar :: Transparent
toolbar.setBackgroundColor(Color.TRANSPARENT);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("StackOverflow");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// Status bar :: Transparent
Window window = this.getWindow();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(Color.TRANSPARENT);
}
...........
..................
}
3。将以下主题应用于 MainActivity
。
values / styles.xml:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
.......
...........
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
AndroidManifest.xml:
<activity
android:name=".EmptyActivity"
android:theme="@style/AppTheme.NoActionBar">
</activity>
输出:
仅供参考,我刚刚在我的内容中使用了地图的示例图片 LinearLayout
显示行为。对于您的情况,将所有内容(搜索栏,地图
)放入其中。
FYI, I have just used an sample image of map in my content LinearLayout
to show the behavior. For your case put all of your content(searchbar, map
) inside it.
希望这会有所帮助〜
这篇关于透明动作栏和优步等状态栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!