【问题标题】:Elevation not working on TabLayout海拔在 TabLayout 上不起作用
【发布时间】:2018-03-05 13:24:39
【问题描述】:

我正在 ViewPager 内的 TabLayout 上设置高度,但它根本没有显示。我在stackoverflow上尝试了很多答案,但无法解决问题。在 CoordinatorLayout 中设置 android:clipToPadding="false" 也不能解决问题。任何帮助,将不胜感激。这是我正在使用但现在得到提升的布局的 xml:

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

    <View
        android:id="@+id/statusBarBackground"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:clipToPadding="false" >

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipToPadding="false" >

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    </ScrollView>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="fixed"
            app:tabGravity="fill"
            app:elevation="3dp" />

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

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior" />

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

【问题讨论】:

  • 只需将 app:elevation="0dp" 添加到 AppBarLayout..
  • @ZeroOne 将 app:elevation="0dp" 添加到 AppBarLayout 不起作用。
  • 哦抱歉.. 将 0dp 更改为其他值,例如:6dp..

标签: android xml android-elevation


【解决方案1】:

将 AppBarLayout android:elevation 更改为 0dp 并将 TabLayout android:elevation 更改为 16dp

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

    <View
        android:id="@+id/statusBarBackground"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:elevation="0dp" >

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipToPadding="false" >

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    </ScrollView>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="fixed"
            app:tabGravity="fill"
            android:elevation="16dp" />

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

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior" />

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

【讨论】:

    【解决方案2】:
    • 要使阴影可见,您必须在 TabLayout 上设置背景。它可以是与窗口背景相同的颜色(只要它是没有 alpha 的纯色)。

    • 您还必须给它Tablayout 边距才能看到海拔。 最小边距应该是你给的elevation。

      android.support.design.widget.TabLayout ... android:elevation="6dp" android:margin="10dp" // margin > elevation android:background="@color/white" />

    【讨论】:

      【解决方案3】:

      使用 CardView 高程; 是的,也许它的 bckle,但它对我有用

      <android.support.v7.widget.CardView
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              app:cardCornerRadius="0dp"
              app:cardElevation="5dp">
              <android.support.design.widget.TabLayout
                       android:id="@+id/tab"
                       android:layout_width="match_parent"
                       android:layout_height="wrap_content"
                       />
      </android.support.v7.widget.CardView>
      

      【讨论】:

        【解决方案4】:

        你的布局有几个问题:

        1. 更改应用程序:elevation="3dp" -> android:elevation="3dp"
        2. 将 android:background="@color/colorPrimary" 添加到您的 TabLayout
        3. 将 android:paddingBottom="3dp" 添加到您的 AppBarLayout

        主要思想是,如果视图背景是透明的或父视图正在剪切您的阴影,您的阴影将不会显示。

        你还有一些问题需要解决,但只是一个开始。

        【讨论】:

        • 仍然没有阴影,并且填充使 TabLayout 上升。
        • 如果您说它导致您的 TabLayout 上升,则意味着正在投下阴影。你怎么会有深度感知?所以你现在的问题是你的 ScrollView 的高度低于你的 TabLayout,这意味着 TabLayout 的阴影在 ScrollView 上。
        • TabLayout 在 ScrollView 下方,所以我认为它们的阴影不会重叠。
        • AppBarLayout 是 LinearLayout 的子类,根据方向从上到下或从左到右放置子类.
        【解决方案5】:

        将 app:elevation="3dp" 更改为 android:elevation="3dp"

        <android.support.design.widget.TabLayout
                    android:id="@+id/tabs"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:tabMode="fixed"
                    app:tabGravity="fill"
                 change ---->  android:elevation="3dp" />
        

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-01-03
        • 2021-05-05
        • 2015-01-11
        • 2023-03-17
        • 2016-05-22
        • 2014-12-25
        • 1970-01-01
        相关资源
        最近更新 更多