Android中的文本阴影效果?

223

任何人都可以查看此链接:https://dev59.com/DnE95IYBdhLWcg3wCJbk#51761610 - Hoque MD Zahidul
4个回答

472

将这些放入values/colors.xml中

<resources>
    <color name="light_font">#FBFBFB</color>
    <color name="grey_font">#ff9e9e9e</color>
    <color name="text_shadow">#7F000000</color>
    <color name="text_shadow_white">#FFFFFF</color>
</resources>

然后在你的布局xml中,以下是一些TextView的示例

浅色背景上带有深色阴影的悬浮文字示例

<TextView android:id="@+id/txt_example1"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:textSize="14sp"
                  android:textStyle="bold"
                  android:textColor="@color/light_font"
                  android:shadowColor="@color/text_shadow"
                  android:shadowDx="1"
                  android:shadowDy="1"
                  android:shadowRadius="2" />

在此输入图片描述

浅色底上带有深色阴影的蚀刻文字示例

<TextView android:id="@+id/txt_example2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="14sp"
                android:textStyle="bold"
                android:textColor="@color/light_font"
                android:shadowColor="@color/text_shadow"
                android:shadowDx="-1"
                android:shadowDy="-1"
                android:shadowRadius="1" />

enter image description here

清晰的文本,浅色带深色阴影的示例

<TextView android:id="@+id/txt_example3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="14sp"
                android:textStyle="bold"
                android:textColor="@color/grey_font"
                android:shadowColor="@color/text_shadow_white"
                android:shadowDx="-2"
                android:shadowDy="-2"
                android:shadowRadius="1" />

在此输入图片描述

注意这些正负值......我建议你自己玩弄颜色/值,但最终你可以调整这些设置以获得你想要的效果。


11
在我的情况下,它在 Android 编辑器预览中不可见,但在真实设备上却像魔法般地运行!!! - Mrinmoy
任何人都可以查看此链接:https://dev59.com/DnE95IYBdhLWcg3wCJbk#51761610 - Hoque MD Zahidul
1
如此扎实且视觉上有良好文档支持的答案。 - Mark Lapasa
有帮助...谢谢 - Pooja
完美运行 - Ashish Choudhary

218

2
这不会做出任何更改。我认为它可以工作。但是我设置的值有问题。你能否发布“shadowdx”、“shadowdy”和“shadowradious”的示例值? - Praveen
19
请发布您的代码。同时,检查诸如 http://mobile.tutsplus.com/tutorials/android/customize-android-fonts/(滚动到文本阴影部分)之类的示例中的标记。 - Pontus Gagge
2
阴影dx、阴影dy和阴影半径的单位是什么? - android developer
4
为什么它们是以像素为单位,而其他所有东西都是以 dp 为单位?这没有意义.. :( - android developer
@androiddeveloper:“如果没有指定,那么单位始终为px。- Romain Guy”https://dev59.com/lW445IYBdhLWcg3wcZ8N - skywall
显示剩余3条评论

68
TextView textv = (TextView) findViewById(R.id.textview1);
textv.setShadowLayer(1, 0, 0, Color.BLACK);

3
似乎忽略了颜色,使用与文本相同的内容。 - jjxtra
任何人都可以查看此链接:https://dev59.com/DnE95IYBdhLWcg3wCJbk#51761610 - Hoque MD Zahidul

2

完整答案

如果视图边界小于阴影偏移量,则需要将这两行添加到父视图中,否则文本阴影将被裁剪。

android:clipChildren="false"

android:clipToPadding="false"

例如:

<androidx.constraintlayout.widget.ConstraintLayout
            android:clipChildren="false"
            android:clipToPadding="false"
           ... >

        <TextView
         style="@style/TextShadowStyle"
          ....
            />

文本阴影样式:

 <style name="TextShadowStyle">
        <item name="android:shadowColor">@color/black</item>
        <item name="android:shadowDx">10</item>
        <item name="android:shadowDy">10</item>
        <item name="android:shadowRadius">5</item>
    </style>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接