Java Android更改材质高程阴影颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29187313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android change Material elevation shadow color
提问by Broadwell
is it possible to change the shadow color produced by the xml elevation property? I want the shadow be dynamically changed by code.
是否可以更改由 xml 高程属性产生的阴影颜色?我希望通过代码动态更改阴影。
采纳答案by Zielony
I know that this question is very old and probably the author doesn't need the answer anymore. I'll just leave it here so others can find it.
我知道这个问题很老了,可能作者不再需要答案了。我会把它留在这里,让其他人可以找到它。
Lollipop's elevation system doesn't support colored shadows.
Lollipop 的高程系统不支持彩色阴影。
But, if you need colored shadows, it's possible to get them using Carbon. It's a kind-of support library for Material Design and in the most recent version there is an option to change shadow color. There's a ton of nice designs on Behance featuring colored shadows and I thought it would be nice to have them despite lack of such feature in Android. It's important to note that colored shadows are emulated on allAndroid versions, on 5.0+ too.
但是,如果您需要彩色阴影,则可以使用 Carbon 来获得它们。它是 Material Design 的一种支持库,在最新版本中可以选择更改阴影颜色。Behance 上有大量带有彩色阴影的漂亮设计,我认为尽管 Android 中缺乏这样的功能,但我认为拥有它们会很好。重要的是要注意彩色阴影在所有Android 版本上都可以模拟,在 5.0+ 上也是如此。
https://github.com/ZieIony/Carbon
https://github.com/ZieIony/Carbon
The following image and code can be found in Carbon's samples.
下面的图片和代码可以在 Carbon 的示例中找到。
Code:
代码:
<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>
"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>
回答by Gauthier
Starting API 28 (Pie) View#setOutlineAmbientShadowColor(int color)and View#setOutlineSpotShadowColor(int color)are available in the View class.
起始 API 28 (Pie) View#setOutlineAmbientShadowColor(int color)和View#setOutlineSpotShadowColor(int color)在 View 类中可用。
If you use elevation on your View, you can use both methods to change the color of the shadow.
如果在视图上使用高程,则可以使用这两种方法来更改阴影的颜色。
回答by user7856586
You can use Shadow Layout. Check my answer.