Android更改材质高程阴影颜色

Bro*_*ell 30 java xml android shadow material-design

是否可以更改xml提升属性产生的阴影颜色?我希望通过代码动态更改阴影.

Zie*_*ony 49

我知道这个问题很老,可能作者不再需要答案了.我会把它留在这里,以便其他人可以找到它.

棒棒糖的仰角系统不支持彩色阴影.

但是,如果你需要彩色阴影,可以使用Carbon来获得它们.它是Material Design的一种支持库,在最新版本中有一个改变阴影颜色的选项.Behance上有很多漂亮的设计,有彩色阴影,我认为尽管在Android中缺少这样的功能,但还是很好.重要的是要注意,所有 Android版本都会模拟彩色阴影,也是5.0+.

https://github.com/ZieIony/Carbon

下面的图像和代码可以在Carbon的样本中找到.

在此输入图像描述

码:

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

    <carbon.widget.Button
        android:layout_width="56dp"
        android:layout_height="56dp"
        android:layout_margin="@dimen/carbon_padding"
        android:background="#ffffff"
        app:carbon_cornerRadius="2dp"
        app:carbon_elevation="8dp"
        app:carbon_elevationShadowColor="@color/carbon_red_700"/>

</carbon.widget.LinearLayout>
Run Code Online (Sandbox Code Playgroud)

"CardView":

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

    <carbon.widget.LinearLayout
        android:layout_width="match_parent"
        android:layout_height="160dp"
        android:layout_margin="@dimen/carbon_margin"
        android:background="#ffffff"
        app:carbon_cornerRadius="2dp"
        app:carbon_elevation="8dp"
        app:carbon_elevationShadowColor="@color/carbon_red_700">

        <carbon.widget.ImageView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:src="@drawable/test_image"/>

        <carbon.widget.TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="test text"/>
    </carbon.widget.LinearLayout>

</carbon.widget.LinearLayout>
Run Code Online (Sandbox Code Playgroud)

  • 您能否详细说明如何使用 Carbon 来创建彩色阴影?我目前无法使其发挥作用。提前致谢。 (2认同)

Gau*_*ier 8

从API 28(Pie)开始,View类中提供了View#setOutlineAmbientShadowColor(int color)View#setOutlineSpotShadowColor(int color)

如果在视图上使用高程,则可以同时使用两种方法来更改阴影的颜色。

  • 除此之外,还可以在主题 xml 中将它们设置为 &lt;item name="android:outlineAmbientShadowColor"&gt;@color/yourAmbientShadow&lt;/item&gt; &lt;item name="android:outlineSpotShadowColor"&gt;@color/yourSpotShadow&lt;/item &gt; (4认同)