android - AppBarLayout elevation 隐藏工具栏

标签 android android-layout android-toolbar

我正在尝试 disable the shadow below a transparent AppBarLayout/Toolbar.这是布局:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="#000">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:background="@android:color/transparent"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/imagePreviewToolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_image_preview" />
</android.support.design.widget.CoordinatorLayout>

我已经尝试过

app:elevation="0dp"

getSupportActionBar().setElevation(0);

但这使得 the UP arrow invisible .我也尝试删除 AppBarLayout 并仅使用工具栏,但问题仍然存在。

谁有解决办法?


编辑:

用这段代码替换 AppBarLayout + Toolbar 组合:

<android.support.v7.widget.Toolbar
    android:id="@+id/imagePreviewToolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@android:color/transparent"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:elevation="0dp"/>

部分 解决了这个问题。现在只有当设备处于横向模式时,工具栏才会变得不可见! Android Studio 布局编辑器在两个方向上都很好地向我显示了工具栏,所以我真的不知道问题出在哪里..

最佳答案

经过一些尝试,我确实发现在这种情况下 ToolBar 应该是最后一个 View ,否则如果它是第一个,那么它后面的 View 将与它重叠,因为它的高度设置为 match_parent 所以尝试这样布局。

布局

<?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"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/imagePreviewToolbar" >

    <ImageView
        android:id="@+id/image_preview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/galacticos"
        android:contentDescription="abc"
        android:scaleType="fitCenter" />

    <LinearLayout
        android:id="@+id/actionBtns"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        android:padding="16dp" >

        <ImageButton
            android:id="@+id/setBackgroundBtn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@null"
            android:contentDescription="abc"
            android:src="@drawable/ic_launcher" />

        <ImageButton
            android:id="@+id/downloadBtn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="?selectableItemBackgroundBorderless"
            android:clickable="true"
            android:contentDescription="abc"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>
</RelativeLayout>

<android.support.v7.widget.Toolbar
    android:id="@+id/imagePreviewToolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:fitsSystemWindows="true"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

并且不要忘记初始化这一切并将您的标题设置为 false 以仅显示工具栏后退按钮:

Activity 代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test);

    Toolbar mToolBar = (Toolbar) findViewById(R.id.imagePreviewToolbar);
    setSupportActionBar(mToolBar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    getSupportActionBar().setHomeButtonEnabled(true);

}

图片 enter image description here

希望我对你有帮助。

关于android - AppBarLayout elevation 隐藏工具栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37778309/

相关文章:

android - 文本字段仍然隐藏在键盘后面 - Titanium Android

android - 将菜单溢出图标设置为白色

java - 使用scrollFlags时自定义工具栏不隐藏

android - Android 2.3.3 上的 ListView 页脚背景

android - 如何清除布局上的内容?

android - 如何在 FragmentActivity 上设置工具栏?

android - 再次在 Activity 生命周期 : onStart is called when it shouldn't be

java - 无法通过一堆 URL(字符串数组)设置墙纸。如何解决这个问题?

android - Android 中简单的数据库访问方法

android - 为什么我的自定义主题不影响我的自定义列表项布局的 TextView ?