gpt4 book ai didi

android - TextView :shadowDx/Dy/Radius in dip?

转载 作者:IT王子 更新时间:2023-10-28 23:33:50 26 4
gpt4 key购买 nike

我们如何在 dip 中指定 TextView 的 shadowDx/Dy/Radius 值?现在我有:

<style name="foo">
<item name="android:shadowDx">-1</item>
<item name="android:shadowDy">-1</item>
<item name="android:shadowRadius">1</item>
</style>

我似乎无法在这些地方指定“1dip”。除了在代码中设置它们并自己进行设备密度乘法之外,还有其他方法吗?

谢谢

最佳答案

我使用自定义 xml 属性创建了一个重写的 TextView 以方便地处理此问题。

也可作为库使用:https://github.com/rongi/text-view-shadow-dips

public class TextViewShadowDips extends TextView {

public TextViewShadowDips(Context context, AttributeSet attrs) {
super(context, attrs);

final TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.TextViewShadowDips);
// App can crash on some devices if shadow radius is more than 25 pixels
// On Samsung Galaxy S6 this crash happens when you copy a text from an input field
// https://stackoverflow.com/questions/4866928/ranges-for-radius-in-shadowradius-and-visiblity-in-textview?lq=1
final float shadowRadius = Math.min(
attributes.getDimension(R.styleable.TextViewShadowDips_shadowRadius, 0f),
25f
);
final float shadowDx = attributes.getDimension(R.styleable.TextViewShadowDips_shadowDx, 0f);
final float shadowDy = attributes.getDimension(R.styleable.TextViewShadowDips_shadowDy, 0f);
final int shadowColor = attributes.getColor(R.styleable.TextViewShadowDips_shadowColor, 0);
if (shadowColor != 0) {
setShadowLayer(shadowRadius, shadowDx, shadowDy, shadowColor);
} else {
getPaint().clearShadowLayer();
}
attributes.recycle();
}

“values”资源文件夹中的attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

<declare-styleable name="TextViewShadowDips">
<attr name="shadowRadius" format="dimension"/>
<attr name="shadowDx" format="dimension"/>
<attr name="shadowDy" format="dimension"/>
<attr name="shadowColor" format="color"/>
</declare-styleable>

</resources>

使用示例:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<my.package.name.TextViewShadowDips
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:shadowColor="#000000"
app:shadowRadius="2dp"
app:shadowDx="2dp"
app:shadowDy="2dp"
/>

</RelativeLayout>

关于android - TextView :shadowDx/Dy/Radius in dip?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4854935/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com