半透明状态栏和工具栏

dyn*_*tem 17 android android-layout material-design android-toolbar android-statusbar

我想整合这样的东西:

在此输入图像描述

我这样做了,但我似乎无法将imageview放在工具栏下面.没有工具栏,我可以在状态栏下进行,但是将这两者结合起来是不可能的.

在此输入图像描述

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:fitsSystemWindows="true"
    tools:context="com.project.android.PhotoActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/photo_tl"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#59000000"
        tools:ignore="UnusedAttribute" />

    <ImageView
        android:id="@+id/photo_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:scaleType="fitStart" />


</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

在我的活动中,我做了以下事情:

  getWindow().getDecorView().setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
Run Code Online (Sandbox Code Playgroud)

我还声明了一个styles-v21.xml文件:

<style name="Project.Photo" parent="Project.Light">
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">#59000000</item>
</style>
Run Code Online (Sandbox Code Playgroud)

并将其设置为PhotoActivity的默认样式.我已经尝试将工具栏放在FrameLayout中,但这样做我的工具栏只是隐藏,如下所示:

在此输入图像描述

提前致谢.

得到了修复,但工具栏与状态栏重叠.反正有没有修复填充?如果我使用android:fitsSystemWindows ="true",状态栏不再是半透明的.

mmB*_*mBs 6

我会删除Toolbar从您的布局和使用的实现ActionBar来自AppCompat.Theme:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>
Run Code Online (Sandbox Code Playgroud)

然后,我将为半透明创建一个新样式ActionBar(在values/styles.xml:

<style name="AppTheme.Transparent" parent="AppTheme">
    <item name="windowActionBarOverlay">true</item>
</style>
Run Code Online (Sandbox Code Playgroud)

并在v21/styles.xml:

<style name="AppTheme.Transparent" parent="AppTheme">
    <item name="windowActionBarOverlay">true</item>
    <item name="android:windowTranslucentStatus">true</item>
</style>
Run Code Online (Sandbox Code Playgroud)

我认为,你的Activity延伸AppCompatActivity所以onCreate()你可以打电话:

要启用后退按钮:

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
Run Code Online (Sandbox Code Playgroud)

用于设置半透明颜色:

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(this, R.color.yourTranslucentColor)));
Run Code Online (Sandbox Code Playgroud)

删除你的ActionBar标题:

getSupportActionBar().setDisplayShowTitleEnabled(false);
Run Code Online (Sandbox Code Playgroud)

更重要的是,我会改变你的根LinearLayout,CoordinatorLayout因为它可以让你更好地控制你的布局(它更强大FrameLayout).

我使用的颜色是:

<color name="yourTranslucentColor">#29000000</color>
Run Code Online (Sandbox Code Playgroud)

当然你应该记得将这个主题应用到你ActivityAndroidManifest.xml:

<activity
    android:name=".ui.activity.YourActivity"
    android:theme="@style/AppTheme.Transparent">
</activity>
Run Code Online (Sandbox Code Playgroud)

通过执行所有这些步骤,您应该得到以下内容:

在此输入图像描述

如果它适合您,请告诉我.


Akh*_*mar 6

如你所说,

"我已经尝试将工具栏放在FrameLayout中,但这样做我的工具栏只是隐藏,就像这样:".


这个问题是添加childView的顺序FrameLayout,你添加工具栏作为第一个孩子,之后你添加了ImageView.这就是图像隐藏工具栏的原因.相反,内部视图的顺序FameLayout应该是这样的

<FrameLayout   
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"       
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:fitsSystemWindows="true"
          tools:context="com.project.android.PhotoActivity">

          <ImageView
              android:id="@+id/photo_image"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:adjustViewBounds="true"
              android:scaleType="fitStart" />

          <android.support.v7.widget.Toolbar
              android:id="@+id/photo_tl"
              android:layout_width="match_parent"
              android:layout_height="?attr/actionBarSize"
              android:background="#59000000"
              tools:ignore="UnusedAttribute" />

    </FrameLayout>
Run Code Online (Sandbox Code Playgroud)

此外,对于API级别> = 19,您可以在style.xml文件中添加此属性以使statusBar透明
<item name="android:windowTranslucentStatus">true</item>
为了使statusBar 后面的内容使用此链接

https://developer.android.com/training/system-ui/status.html#behind


Bre*_*asy 1

LinearLayout会自动将其放置ImageViewToolbar.

尝试使用 aRelativeLayout代替。