将这些放入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" />
清晰的文本,浅色带深色阴影的示例
<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" />
注意这些正负值......我建议你自己玩弄颜色/值,但最终你可以调整这些设置以获得你想要的效果。
也许您可以考虑使用android:shadowColor、android:shadowDx
、android:shadowDy
和android:shadowRadius
,或者使用setShadowLayer()方法?
TextView textv = (TextView) findViewById(R.id.textview1);
textv.setShadowLayer(1, 0, 0, Color.BLACK);
完整答案
如果视图边界小于阴影偏移量,则需要将这两行添加到父视图中,否则文本阴影将被裁剪。
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>