标签: android-elevation

如何为canvas绘制的元素添加材质风格的阴影?

正如文档所述,您可以通过添加和自定义材料设计阴影样式android:elevation. This works fine with all elements I came across yet.

但是,如果我想为在 上绘制的形状获得相同(或几乎相同)的行为,该怎么办Canvas?- 目前我正在玩Paint.setShadowLayer(), but the results are far from 'material'-style compared with the native implementation.

有没有任何库或方法可以达到相同的效果?最好添加海拔高度?

android canvas shadow material-design android-elevation

5
推荐指数
1
解决办法
2104
查看次数

Android:设置上方元素的高度(阴影向上方向)

在页脚按钮和主滚动视图之间获得阴影的正确方法是什么?

Chrome 示例

这是我当前的布局:

布局示意图

将高程属性应用于最新的RelativeLayout不起作用:

layout android material-design android-elevation

5
推荐指数
0
解决办法
1205
查看次数

NestedScrollView 中的 CardView 自动增加高度 [包含图像]

我正在嵌套滚动视图中添加卡片。对于 LinearLayout 中顺序较低的卡片,卡片高度会自动增加。要了解该问题,请注意第一张卡片和最后一张卡片之间的卡片高度。尽管卡片相同,但最后一张卡片的阴影比第一张卡片的阴影更深。如何解决?

使用 appcompat v7:22.+ 作为 v7 库的依赖项。

这是我的卡片视图布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardElevation="1dp"
    android:layout_marginBottom="10dp"
    card_view:cardUseCompatPadding="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_margin="10dp"
            xmlns:android="http://schemas.android.com/apk/res/android">

            <TextView
                android:id="@+id/hint"
                android:layout_width="0dp"
                android:text="ABC"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textColor="@color/textHint"/>
            <TextView
                android:id="@+id/description"
                android:layout_width="0dp"
                android:text="DEFG BHJHKJ"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:layout_weight="1"/>
        </LinearLayout>

</android.support.v7.widget.CardView>
Run Code Online (Sandbox Code Playgroud)

这就是我使用卡片视图的方式:

<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:fitsSystemWindows="true">

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

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsingtoolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleTextAppearance="@android:color/transparent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <include
                layout="@layout/shared_activity_item"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_collapseMode="parallax" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:contentInsetStart="0dp"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> …
Run Code Online (Sandbox Code Playgroud)

android android-scrollview android-cardview android-elevation nestedscrollview

5
推荐指数
0
解决办法
1598
查看次数

在ImageButton中使用高程和纹波

我正在尝试在ImageButton上同时设置高程和涟漪效果.我正在运行LOLLIPOP.

这是代码:

    <android.support.v7.widget.AppCompatImageButton
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:elevation="5dp"
            android:background="?attr/selectableItemBackgroundBorderless"
            app:srcCompat="@drawable/add" />
Run Code Online (Sandbox Code Playgroud)

我的主题是: Theme.AppCompat.Light.DarkActionBar

有了这段代码,我只能得到涟漪.如果我删除背景(波纹),我得到提升.

编辑:

注意:所有屏幕截图都是在点击图像的情况下拍摄的(长按).

我已经测试了android:background="?attr/selectableItemBackground",我得到了一个波纹,背景是一个正方形,但没有高程:

android:background="?attr/selectableItemBackgroundBorderless"我买了圆形的背景波纹,但没有海拔:

没有背景属性我得到了高程,默认ImageButton背景:

我需要的是圆形波纹背景和高度同时.

android android-5.0-lollipop android-elevation

5
推荐指数
1
解决办法
1431
查看次数

Android导航视图-项目高程

有没有一种方法,可以使用Google支持库的NavigationView元素,在NavigationView内的项目上使用高程来实现以下效果:

带标头高程的导航视图

您可以看到导航抽屉的标题具有高程,就像视图底部的页脚一样。

我尝试使用“ android:elevation”属性,但它似乎无法正常工作。
这是我目前对NavigationView的代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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"
    tools:openDrawer="end">

    <include
        layout="@layout/app_bar_product_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_filters">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:gravity="center|end"
            android:padding="16dp"
            android:elevation="16dp"
            android:orientation="horizontal"
            tools:targetApi="lollipop">

            <TextView
                android:textAppearance="@style/TextAppearance.AppCompat.Button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Clear"/>

        </LinearLayout>

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

</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)

这是导航视图标题的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorAccent"
    android:elevation="8dp"
    android:gravity="bottom"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:theme="@style/ThemeOverlay.AppCompat.Light"
    tools:targetApi="lollipop">

    <android.support.v4.widget.Space
        android:layout_width="match_parent"
        android:layout_height="@dimen/status_cheat" />

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/ordina_per"
            android:textAppearance="@style/TextAppearance.AppCompat.Subhead"
            android:textColor="@android:color/white" …
Run Code Online (Sandbox Code Playgroud)

xml android android-layout navigation-drawer android-elevation

5
推荐指数
1
解决办法
734
查看次数

使用可绘制视图 setBackground 删除高程的阴影

我尝试将 Material Design 的海拔概念应用到我的 RecyclerView 中。

对于 RecyclerView 的每个项目我都使用这个

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    card_view:cardCornerRadius="0dp"
    android:elevation="2dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="8dp">
        <TextView
        .../>

        <CheckBox
        .../>
    </RelativeLayout>
</android.support.v7.widget.CardView>
Run Code Online (Sandbox Code Playgroud)

当我选择项目时,我以编程方式将可绘制对象应用到背景以实现效果,并按照 Material Design 规范的建议将高度设置为 8dp。

        val draw = ResourcesCompat.getDrawable(resources, R.drawable.selected_task, null)
        draw?.setColorFilter(task.color.aRGB.toInt(), PorterDuff.Mode.OVERLAY)
        view.background = draw
        view.elevation = 8F
Run Code Online (Sandbox Code Playgroud)

(我用 Kotlin 编写代码)可绘制对象:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke
        android:width="2dp"
        android:color="@color/select_border"/>
    <solid
        android:color="@color/select_fill"/>
</shape>
Run Code Online (Sandbox Code Playgroud)

和颜色值

<color name="select_border">#78000000</color>
<color name="select_fill">#32000000</color>
Run Code Online (Sandbox Code Playgroud)

问题是,当我选择一项时,可绘制对象被很好地应用,但阴影被完全删除并且没有放大!如果我不使用可绘制对象设置背景,则效果很好。阴影像预期的那样被放大。

问题是什么 ?

android kotlin android-drawable material-design android-elevation

5
推荐指数
1
解决办法
1034
查看次数

Android海拔-相同的值不同的阴影

我在回收器视图中设置项目标高时遇到了一个奇怪的错误。我在适配器中有两种项目类型,第一种是一种具有复杂布局的仪表板,具有三个cardview。第二个是数据列表的经典项目,其中每个项目都以卡片视图作为根。两种布局中的每个卡片视图定义如下

第一个项目类型:

<LinearLayout
        android:id="@+id/root"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="4dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="8dp"
            app:cardCornerRadius="2dp"
            app:cardElevation="3dp"
            >

        ....

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

    ....

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

第二个项目类型:

<android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        app:cardCornerRadius="2dp"
        app:cardElevation="3dp"
        >

    ....

</android.support.v7.widget.CardView>
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,两个Cardview都具有相同的标高设置,但是当我部署此布局时,它们具有不同的阴影,知道这可能是什么原因吗?

我切割了屏幕的一部分并将切口彼此相邻放置,以便您可以看到问题...... 在此输入图像描述

编辑

第一个项目的布局非常复杂,但该项目类型中的所有三个CardView都具有相同的阴影(上图中左侧的阴影)。第二种项目类型的根卡片视图具有图像右侧的阴影。

default_elevation = 3dp 
default_corner_radius = 2dp
margins are 16dp, 8dp, 4dp etc ...
Run Code Online (Sandbox Code Playgroud)

第一个项目类型:

<layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <LinearLayout
        android:id="@+id/root"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/margin_and_half"
            android:layout_marginLeft="@dimen/default_padding"/>

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/margin_quarter"
            android:layout_marginLeft="@dimen/margin_half"
            android:layout_marginRight="@dimen/margin_half"
            android:layout_marginBottom="@dimen/margin_half"
            app:cardCornerRadius="@dimen/default_corner_radius" …
Run Code Online (Sandbox Code Playgroud)

android shadow android-cardview android-elevation android-recyclerview

4
推荐指数
1
解决办法
1690
查看次数

AppBarLayout上的提升不起作用

当我尝试为AppBarLayout的高程设置特定值时,阴影完全消失.

 <android.support.design.widget.AppBarLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     app:elevation="4dp">

     <!-- Toolbar -->
     <android.support.v7.widget.Toolbar...

     <!-- Other Layouts -->

</android.support.design.widget.AppBarLayout>
Run Code Online (Sandbox Code Playgroud)

这是一个错误还是预期的行为?

我正在使用设计库的26.0.0版本.

android android-toolbar android-elevation android-design-library

4
推荐指数
1
解决办法
2975
查看次数

如何解决android的react-native中高程阴影问题

我正在尝试在 react-native 中显示视图周围的阴影。

我试过这个

<View
      style={{
        flex: 0.3,
        margin: 4,
        ...Platform.select({
          ios: {
            shadowColor: '#000',
            shadowOffset: {width: 0, height: 4},
            shadowOpacity: 0.4,
          },
          android: {
            elevation: 2,
          },
        }),
      }}>
Run Code Online (Sandbox Code Playgroud)

它在 iOs 上完美运行,但在 android 上它没有效果。有一个名为 react-native-shadow 的库,它在 iOs 和 android 上都可以完美运行,但是如何在不使用任何第三方库的情况下修复 android 中的阴影问题。

android android-elevation react-native

4
推荐指数
1
解决办法
3049
查看次数

Android - Elevation无效

我正在迁移Android应用程序以使用Material Design.为了验证我是否所做的一切正确,我创建了一个空活动来测试一些Material Design itens(FAB,凸起按钮,视图上的高程,卡片,列表等等).

按钮显示为OK,但视图的高程不是.我在互联网上搜索了一个答案,但没有找到答案.我没有使用填充,透明背景等.这是我的代码:

值-V21/styles.xml:

<resources>

    <style name="AppThemeNoActionBar" parent="AppTheme">
        <item name="android:navigationBarColor">@color/colorPrimary</item>
    </style>

    <style name="AppTheme" 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>

    <style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
        <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>
    </style>

    <style name="Button" parent="Base.Widget.AppCompat.Button.Colored">
        <item name="android:backgroundTint">@color/colorAccent</item>
    </style>

    <color name="colorPrimary">#E88768</color>
    <color name="colorPrimaryDark">#CC785C</color>
    <color name="colorAccent">#E88768</color>
    <color name="textColorPrimary">#000000</color>
    <color name="windowBackground">#FFFFFF</color>
    <color name="navigationBarColor">#E88768</color>
</resources>
Run Code Online (Sandbox Code Playgroud)

布局-V21/start_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/white"
                xmlns:app="http://schemas.android.com/apk/res-auto">
    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_alignParentStart="true"
        android:elevation="12dp"
        android:translationZ="12dp"
        android:id="@+id/scrollView">

    </HorizontalScrollView>
    <LinearLayout
        android:layout_width="match_parent" …
Run Code Online (Sandbox Code Playgroud)

android android-layout material-design android-elevation

3
推荐指数
1
解决办法
5921
查看次数