appcompat和设计库中没有阴影

时间:2015-09-24 15:50:53

标签: android

我的问题是我的应用中没有阴影。我使用的是AppCompat LibaryDesign Library。在预览(android studio)中有阴影,但我无法通过手机在我的应用程序中看到。

这是我的layout code

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

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:theme="@style/ThemeToolbar" />
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

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


            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/cardViewMarginTopBottom"
                android:layout_marginLeft="@dimen/cardViewMarginSlide"
                android:layout_marginRight="@dimen/cardViewMarginSlide"
                android:layout_marginTop="@dimen/cardViewMarginTopBottom"
                android:padding="@dimen/cards_padding"
                app:cardBackgroundColor="@color/icons"
                app:cardCornerRadius="@dimen/cardCornerRadius"
                app:cardElevation="@dimen/cardElevation"
                app:cardMaxElevation="@dimen/cardMaxElevation">

                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="@dimen/cards_margin"
                    android:orientation="vertical">


                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="@dimen/cards_text_margin"
                        android:lineSpacingMultiplier="1.2"
                        android:text="Data"
                        android:textColor="@color/primary"
                        android:textSize="@dimen/text_big" />


                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal">

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_margin="@dimen/cards_text_margin"
                            android:lineSpacingMultiplier="1.2"
                            android:text="X: "
                            android:textColor="@color/primary_text"
                            android:textSize="@dimen/text_small"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/textViewFragmentHome1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_margin="@dimen/cards_text_margin"
                            android:lineSpacingMultiplier="1.2"
                            android:textColor="@color/primary_text"
                            android:textSize="@dimen/text_small" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal">

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_margin="@dimen/cards_text_margin"
                            android:lineSpacingMultiplier="1.2"
                            android:text="Y: "
                            android:textColor="@color/primary_text"
                            android:textSize="@dimen/text_small"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/textViewFragmentHome2"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_margin="@dimen/cards_text_margin"
                            android:lineSpacingMultiplier="1.2"
                            android:textColor="@color/primary_text"
                            android:textSize="@dimen/text_small" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal">

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_margin="@dimen/cards_text_margin"
                            android:lineSpacingMultiplier="1.2"
                            android:text="Z: "
                            android:textColor="@color/primary_text"
                            android:textSize="@dimen/text_small"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/textViewFragmentHome3"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_margin="@dimen/cards_text_margin"
                            android:lineSpacingMultiplier="1.2"
                            android:textColor="@color/primary_text"
                            android:textSize="@dimen/text_small" />
                    </LinearLayout>

                </LinearLayout>

            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/cardViewMarginTopBottom"
                android:layout_marginLeft="@dimen/cardViewMarginSlide"
                android:layout_marginRight="@dimen/cardViewMarginSlide"
                android:layout_marginTop="@dimen/cardViewMarginTopBottom"
                android:padding="@dimen/cards_padding"
                app:cardBackgroundColor="@color/icons"
                app:cardCornerRadius="@dimen/cardCornerRadius"
                app:cardElevation="@dimen/cardElevation"
                app:cardMaxElevation="@dimen/cardMaxElevation">

                <com.github.mikephil.charting.charts.LineChart
                    android:id="@+id/lineChartFragmentHome1"
                    android:layout_width="match_parent"
                    android:layout_height="250dp" />

            </android.support.v7.widget.CardView>


            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/cardViewMarginTopBottom"
                android:layout_marginLeft="@dimen/cardViewMarginSlide"
                android:layout_marginRight="@dimen/cardViewMarginSlide"
                android:layout_marginTop="@dimen/cardViewMarginTopBottom"
                android:padding="@dimen/cards_padding"
                app:cardBackgroundColor="@color/icons"
                app:cardCornerRadius="@dimen/cardCornerRadius"
                app:cardElevation="@dimen/cardElevation"
                app:cardMaxElevation="@dimen/cardMaxElevation"
                app:cardUseCompatPadding="true">

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

                    <android.support.v7.widget.AppCompatCheckBox
                        android:id="@+id/checkboxFragmentHome1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="@dimen/cards_text_margin"
                        android:checked="true"
                        android:text="Show X line"
                        android:textSize="@dimen/text_med" />

                    <android.support.v7.widget.AppCompatCheckBox
                        android:id="@+id/checkboxFragmentHome2"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="@dimen/cards_text_margin"
                        android:checked="true"
                        android:text="Show Y line"
                        android:textSize="@dimen/text_med" />

                    <android.support.v7.widget.AppCompatCheckBox
                        android:id="@+id/checkboxFragmentHome3"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="@dimen/cards_text_margin"
                        android:checked="true"
                        android:text="Show Z line"
                        android:textSize="@dimen/text_med" />

                </LinearLayout>
            </android.support.v7.widget.CardView>


            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/cardViewMarginTopBottom"
                android:layout_marginLeft="@dimen/cardViewMarginSlide"
                android:layout_marginRight="@dimen/cardViewMarginSlide"
                android:layout_marginTop="@dimen/cardViewMarginTopBottom"
                android:padding="@dimen/cards_padding"
                app:cardBackgroundColor="@color/icons"
                app:cardCornerRadius="@dimen/cardCornerRadius"
                app:cardElevation="@dimen/cardElevation"
                app:cardMaxElevation="@dimen/cardMaxElevation">

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

                    <android.support.v7.widget.SwitchCompat
                        android:id="@+id/switchFragmentHome1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="@dimen/cards_text_margin"
                        android:checked="false"
                        android:text="Show X axis lines number"
                        android:textSize="@dimen/text_med" />

                    <android.support.v7.widget.SwitchCompat
                        android:id="@+id/switchFragmentHome2"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="@dimen/cards_text_margin"
                        android:checked="false"
                        android:text="Just do it slowly.."
                        android:textSize="@dimen/text_med" />

                </LinearLayout>
            </android.support.v7.widget.CardView>

        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fabFragmentHome"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="@dimen/floatingActionButtonMargin"
        android:scaleType="center"
        android:src="@drawable/ic_av_pause"
        app:layout_behavior="com.eternal.arcanus.accelerometer.helpers.FloatingButtonScroll" />
</android.support.design.widget.CoordinatorLayout>

这是android studio preview:

enter image description here

手机显示了这个:

enter image description here

我做错了什么?

2 个答案:

答案 0 :(得分:2)

不幸的是没有默认的阴影(或者至少尽可能接近,我很高兴得到纠正)。但是,在您的情况下,至少有一种解决方法。

首先,创建一个drawable xml文件来表示您的影子(即app_bar_shadow.xml):

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">    
    <gradient
        android:startColor="#02444444"
        android:endColor="#33111111"
        android:angle="90"/>
</shape>

然后将其添加到layout,并为其指定滚动行为。将它放在NestedScrollView文件中的layout下面:

<View
    android:layout_width="match_parent"
    android:layout_height="4dp"
    android:id="@+id/app_bar_shadow"
    android:layout_gravity="top"
    android:background="@drawable/app_bar_shadow"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

答案 1 :(得分:0)

从棒棒糖开始可以使用Shadow,你使用的手机/模拟器是否有较旧的android?不幸的是appcompat并没有解决旧机器人的这个问题