【问题标题】:How can I create a shadow in a TextView?如何在 TextView 中创建阴影?
【发布时间】:2018-07-01 10:53:37
【问题描述】:

我有一个要求在 TextView 中创建阴影?

我怎样才能实现它?附加的屏幕排序。

如果有任何想法,请告诉我。

谢谢!!!提前。

【问题讨论】:

  • 你会在你的Android Studio中找到shadowDxshadowDyshadowRadiusshadowColor方法,使用它们
  • 有什么问题?

标签: android android-layout layout textview


【解决方案1】:

在您的 XML 中添加海拔属性。将其设置为 5dp。

<TextView
    android:id="@+id/myText"
    ...
    android:elevation="5dp" />

【讨论】:

  • 这不会导致提问者的形象!!而且它在 Android 版本中也有限制,它只能在 Lollipop+ 中运行
【解决方案2】:

API>=21 可以直接使用CustomViewOutlineProvider

CustomViewOutlineProvider.java

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class CustomViewOutlineProvider extends ViewOutlineProvider {
    int roundCorner;

    public CustomViewOutlineProvider(int round) {
        roundCorner = round;
    }

    @Override
    public void getOutline(View view, Outline outline) {
        outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), roundCorner);
    }
}

活动

    TextView textView = (TextView) findViewById(R.id.shadow_txt);
    textView.setOutlineProvider(new CustomViewOutlineProvider(30));
    textView.setClipToOutline(true);

对于 Prelollipop 设备(API

    <TextView
        android:background="@drawable/btn_with_shadow"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Font Style"
        android:textColor="@color/white"
        android:paddingRight="24dp"
        android:paddingLeft="24dp"
        android:paddingTop="12dp"
        android:paddingBottom="12dp"
        />

btn_with_shadow.xml

<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/btn_shadow"/>
    <item
        android:drawable="@drawable/bg_button"
        android:bottom="4px"
        android:top="3px"
        android:right="4px"
        android:left="3px"/>

</layer-list>

btn_shadow.xml

<shape android:shape="rectangle"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <solid android:color="@color/alpha_15"/>
    <corners android:radius="20dip"/>

</shape>

bg_button.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid android:color="@color/colorPrimary"/>
    <corners android:radius="20dp" />

</shape>

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>

    <color name="alpha_15">#26000000</color>
</resources>

结果 -

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-24
    • 2011-11-12
    • 2018-02-14
    • 1970-01-01
    • 2016-09-18
    • 1970-01-01
    相关资源
    最近更新 更多