java - 向 Android TextView 添加不透明的 "shadow"(轮廓)

标签 java android textview drawing mapsforge

我有一个 TextView在我的 Activity我想添加阴影。它应该看起来像 OsmAnd (100% 不透明):

what I want

但它看起来像这样:

What I have

可以看到当前的阴影变模糊了,逐渐消失了。我想要一个坚实的、不透明的阴影。但是如何呢?

我当前的代码是:

<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 , herehere .

它们都没有那么好用或看起来那么好。

现在这就是你真正的做法(在 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);
    }
}

结果是这样的:

result screenshot

关于java - 向 Android TextView 添加不透明的 "shadow"(轮廓),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39106454/

相关文章:

java - hibernate.hbm2ddl.auto 自动不添加新列

java - 创建成员变量的实例

xml - 在 Android 中存储和访问 XML 的最佳方式是什么?

java - 在处理器的密集工作之前显示 toast

android - 动态 Textview 滚动条不显示

android - 在 android 文本切换器或 View 翻转器中将大文本分成页面

java - 如何以1秒的间隔执行一段代码?

java - 无效的可绘制标签 vector - appcompat-v7 :24. 1.1

android - Android的RelativeLayout和iOS的AutoLayout有什么区别?

android - 无法在android xml中设置 TextView 的文本颜色