+-
android – 半透明状态栏和工具栏
我想整合这样的东西:

enter image description here

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

enter image description here

这是我的布局:

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

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

  getWindow().getDecorView().setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);

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

<style name="Project.Photo" parent="Project.Light">
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">#59000000</item>
</style>

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

enter image description here

提前致谢.

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

最佳答案
我将从您的布局中删除工具栏并使用AppCompat中的ActionBar实现.

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

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

<style name="AppTheme.Transparent" parent="AppTheme">
    <item name="windowActionBarOverlay">true</item>
</style>

在v21 / styles.xml中:

<style name="AppTheme.Transparent" parent="AppTheme">
    <item name="windowActionBarOverlay">true</item>
    <item name="android:windowTranslucentStatus">true</item>
</style>

我假设您的Activity扩展了AppCompatActivity,因此在onCreate()中您可以调用:

要启用后退按钮:

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);

用于设置半透明颜色:

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(this, R.color.yourTranslucentColor)));

要删除ActionBar标题:

getSupportActionBar().setDisplayShowTitleEnabled(false);

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

我使用的颜色是:

<color name="yourTranslucentColor">#29000000</color>

当然,您应该记住将此主题应用于AndroidManifest.xml中的Activity:

<activity
    android:name=".ui.activity.YourActivity"
    android:theme="@style/AppTheme.Transparent">
</activity>

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

enter image description here

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

点击查看更多相关文章

转载注明原文:android – 半透明状态栏和工具栏 - 乐贴网