Add opaque quot;shadowquot; (outline) to Android TextView(添加不透明的“阴影(大纲)到 Android TextView)
问题描述
我有一个
但它看起来像这样:
您可以看到当前阴影模糊并逐渐消失.我想要一个坚实的、不透明的阴影.但是怎么做呢?
我当前的代码是:
我尝试了其他帖子中的所有技巧、技巧和窍门,例如
I have a TextView
in my Activity to which I want to add a shadow. It is supposed to look like in OsmAnd (100% opaque):
But it looks like this:
You can see that the current shadow is blurred and fades away. I want a solid, opaque shadow. But how?
My current code is:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/speedTextView"
android:text="25 km/h"
android:textSize="24sp"
android:textStyle="bold"
android:textColor="#000000"
android:shadowColor="#ffffff"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="6"
/>
I tried all the hacks, tips and tricks in the other posts like here, here and here.
None of them works that great or looks so good.
Now this is how you really do it (found in the Source of the OsmAnd app):
You use a FrameLayout (which has the characteristic of laying its components over each other) and put 2 TextViews inside at the same position.
MainActivity.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#445566">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_weight="1">
<TextView
android:id="@+id/textViewShadowId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:textSize="36sp"
android:text="123 ABC"
android:textColor="#ffffff" />
<TextView
android:id="@+id/textViewId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:textSize="36sp"
android:text="123 ABC"
android:textColor="#000000" />
</FrameLayout>
</LinearLayout>
And in the onCreate
method of your activity you set the stroke width of the shadow TextView and change it from FILL to STROKE:
import android.graphics.Paint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//here comes the magic
TextView textViewShadow = (TextView) findViewById(R.id.textViewShadowId);
textViewShadow.getPaint().setStrokeWidth(5);
textViewShadow.getPaint().setStyle(Paint.Style.STROKE);
}
}
The result looks like this:
这篇关于添加不透明的“阴影"(大纲)到 Android TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:添加不透明的“阴影"(大纲)到 Android TextView
基础教程推荐
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 降序排序:Java Map 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01