在Android中,如何为文本添加边框?我想要在TextView中的文本周围添加边框,而不是在TextView周围。
是否有任何方法可以实现?如果有,请指出所需的正确步骤。
试试这个...
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:textColor="#f8f36a"
android:textSize="65sp"
android:shadowColor="#ff0000"
android:shadowRadius="2"/>
结果是:
你可以像这样为文本创建自定义边框..这里有一个示例应用程序。
你可以为这个创建样式..
这里有一个文本边框的示例样式。
Style.xml
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="shadoweffect">
<item name="android:paddingLeft">4px</item>
<item name="android:paddingBottom">4px</item>
<item name="android:textColor">#FFFFFF</item>
<item name="android:textSize">25sp</item>
<item name="android:shadowColor">#000000</item>
<item name="android:shadowDx">0</item>
<item name="android:shadowDy">0</item>
<item name="android:shadowRadius">3</item>
</style>
</resources>
在TextView上应用样式
<TextView
style="@style/shadoweffect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="your_text" />
Android原本没有这个功能。几个月前,我不得不开发类似的功能,我使用了一些技巧。在那种情况下,我使用了shadow
属性。在你的情况下,在TextView.onDraw()
调用中两次调用super()
。将字体设置为粗体,并将颜色设置为边框颜色并调用一次,然后再将其设置为普通并调用第二次。那样就可以实现。
您可以使用任何文本编辑器为您的文本创建边框,并将其添加到代码中。