应用程序启动后隐藏阴影效果(高程)

时间:2016-09-14 09:52:48

标签: java android android-studio material-design

我的应用程序中的提升效果有问题(TabLayout下的阴影),在我运行应用程序后崩溃了。它在预览模式下可见,但在我的设备上不可见,因为它隐藏了。这是我的代码:

主要活动

<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"
android:background="@android:color/white"
android:clipToPadding="false">

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

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/toolbar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed" >

        <ImageView
            android:id="@+id/header"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/background_portrait"
            android:fitsSystemWindows="true"
            android:scaleType="centerCrop"
            app:layout_collapseMode="parallax" />`

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

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabGravity="fill"
        android:elevation="20dp"
        app:tabTextAppearance="@style/MineCustomTabText"
        />

</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="@string/appbar_scrolling_view_behavior"
    /></android.support.design.widget.CoordinatorLayout>

这是styles.xml的代码

<resources>

<style name="MyMaterialTheme" parent="MyMaterialTheme.Base">

</style>

<style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="colorControlNormal">@color/editTextIndigo</item>
    <item name="colorControlActivated">@color/editTextPink</item>
</style>

<style name="Toolbar.TitleText" parent="TextAppearance.Widget.AppCompat.Toolbar.Title">
    <item name="android:textSize">16sp</item>
    <item name="android:textAllCaps">true</item>
</style>
<style name="MineCustomTabText" parent="TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse">
    <item name="android:textSize">13sp</item>
    <item name="android:textAllCaps">true</item>
    <item name="android:textColor">@android:color/white</item>
</style>

样式-v21.xml

<resources>

<style name="MyMaterialTheme" parent="MyMaterialTheme.Base">
    <item name="android:windowContentTransitions">true</item>
    <item name="android:windowAllowEnterTransitionOverlap">true</item>
    <item name="android:windowAllowReturnTransitionOverlap">true</item>
    <item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
    <item name="android:windowSharedElementExitTransition">@android:transition/move</item>
</style>

这是运行app时的屏幕:

via GIPHY

我希望实现一个简单的阴影效果,它不会隐藏在tablayout下,与其他内容分开。我正在使用android 6.0.1在手机上测试它

1 个答案:

答案 0 :(得分:0)

我找到了解决问题的方法。我创建了相对布局,里面有模仿阴影的视图。在TabLayout之后和AppBarLayout结束之前添加这行代码解决了所有问题。

 <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_anchor="@id/tabs"
        app:layout_anchorGravity="bottom"
        android:background="@android:color/white">


        <View
            android:layout_width="match_parent"
            android:layout_height="4dp"
            android:layout_alignParentBottom="true"
            android:background="@drawable/tablayout_shadow"
            />
    </RelativeLayout>

这是main_activity.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"
android:background="@android:color/white"
android:clipToPadding="false">

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

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/toolbar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed" >

        <ImageView
            android:id="@+id/header"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/background_portrait"
            android:fitsSystemWindows="true"
            android:scaleType="centerCrop"
            app:layout_collapseMode="parallax" />



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



    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabGravity="fill"
        android:elevation="20dp"
        app:tabTextAppearance="@style/MineCustomTabText"
        />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_anchor="@id/tabs"
        app:layout_anchorGravity="bottom"
        android:background="@android:color/white">


        <View
            android:layout_width="match_parent"
            android:layout_height="4dp"
            android:layout_alignParentBottom="true"
            android:background="@drawable/toolbar_shadow"
            />
    </RelativeLayout>
</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="@string/appbar_scrolling_view_behavior"
    />

tablayout_shadow.xml

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

最后经过2天的头脑风暴,我解决了它:D。我希望这将有助于将来。