我有一个 TextView
在我的 Activity我想添加阴影。它应该看起来像 OsmAnd (100% 不透明):
但它看起来像这样:
可以看到当前的阴影变模糊了,逐渐消失了。我想要一个坚实的、不透明的阴影。但是如何呢?
我当前的代码是:
<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"
/>
最佳答案
我尝试了其他帖子中的所有技巧、提示和技巧,例如 here , here和 here .
它们都没有那么好用或看起来那么好。
现在这就是你真正的做法(在 OsmAnd 应用程序的源代码中找到):
您使用一个 FrameLayout(具有将其组件相互重叠的特性)并在同一位置放置 2 个 TextView。
主 Activity .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>
然后在 Activity 的 onCreate
方法中设置阴影 TextView 的笔划宽度并将其从 FILL 更改为 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);
}
}
结果是这样的:
关于java - 向 Android TextView 添加不透明的 "shadow"(轮廓),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39106454/