【问题标题】:tablayout shadow/elevation for pre-lollipop棒棒糖前的 tablayout 阴影/高程
【发布时间】:2017-01-29 00:28:39
【问题描述】:

我在 AppBarLayout 中有一个 tablayout。我正在尝试在棒棒糖前版本的 tabbarlayout 下方添加阴影视图。阴影视图出现了,但是当我向下滚动时,阴影视图似乎不透明。看起来好像在 tablayout 下面有一个填充,在 tabindicator 和 shadow 之间。但是,如果我在工具栏下方使用相同的阴影视图,则向上滚动时它似乎是透明的。有人可以告诉我为标签布局添加阴影是否有什么不同?

main_layout-

<android.support.v4.widget.DrawerLayout     
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/drawer_layout"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:fitsSystemWindows="true">

 <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:orientation="vertical">

        <include layout="@layout/include_coordinator_layout"/>

    </LinearLayout>

    <TextView
        android:id="@+id/mtext_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:visibility="gone"/>

    <Button
        android:id="@+id/mBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_below="@id/mtext_view"
        android:text="@string/retry_button"
        android:visibility="gone"/>
</RelativeLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/navView"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@color/white"
    android:fitsSystemWindows="true" />

</android.support.v4.widget.DrawerLayout>

这是我的 include_coordinator_layout

<?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:id="@+id/main_content"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
       android:id="@+id/appbar"
       android:layout_width="match_parent"
       android:layout_height="wrap_content">

    <include
        android:id="@+id/toolbar"
        layout="@layout/tool_bar" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMaxWidth="0dp"
        app:tabGravity="fill"
        app:tabMode="fixed"
        android:background="@color/tab_layout_color"/>

    <View
        android:id="@+id/shadow_view"
        android:layout_width="match_parent"
        android:layout_height="5dp"
        android:background="@drawable/toolbar_shadow"
        />

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

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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

这是我的toolbar_shadow 可绘制对象。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="@android:color/transparent"
        android:endColor="#88333333"
        android:angle="90"/>
</shape>

【问题讨论】:

    标签: android toolbar android-4.4-kitkat android-tablayout android-elevation


    【解决方案1】:

    我猜你的布局是 LinearLayout

    要么

    将LinearLayout改为RelativeLayout并在你的ViewPager中添加android:layout_below="@+id/appbar"android:paddingTop="-5dp"android:clipToPadding="false"(或者使用ConstraintLayout之类的方法),切换toolbar和view pager的顺序

    将其更改为 FrameLayout,重新排列视图,使 ViewPager 位于第一位,然后将 android:layout_marginTop="@dimen/sizeOfToolBarAndTabsWithoutShadow" 放入其中 dimen 是工具栏和选项卡的设置大小(没有阴影)。您可能想要这样做,因为我似乎记得一些带有一些滚动视图和 clipToPadding 的视觉伪影

    为什么?

    因为AppBarLayout 视图的大小包括您的阴影,所以任何基本视图组 (LinearLayout) 都不知道如何放置下一个视图 (您的 viewpager) 以便阴影与内容略微重叠 - 这有最后绘制,这样视图分页器的顶部就不会覆盖阴影

    【讨论】:

    • 抱歉包含了整个代码。你能告诉我在哪里把它改成 FrameLayout,因为它在 CoordinatorLayout 中?
    猜你喜欢
    • 2015-09-22
    • 2016-10-14
    • 2015-08-12
    • 2015-09-15
    • 2016-11-02
    • 2015-02-26
    • 2015-02-23
    • 2016-08-14
    • 1970-01-01
    相关资源
    最近更新 更多