【问题标题】:Can't set TextView shadow programmatically无法以编程方式设置 TextView 阴影
【发布时间】:2012-08-08 09:56:38
【问题描述】:

我正在动态创建一个 TextView 并使用此处发布的方法为其设置阴影:Android - shadow on text?

但它不起作用。样式已应用(将 textSize 项进行测试,它可以工作),但没有出现阴影。

文本视图:

TextView tv = new TextView(this);
RelativeLayout.LayoutParams layoutPars = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutPars.addRule(RelativeLayout.CENTER_VERTICAL); 
tv.setTextColor(0xffffffff);
tv.setText(label);
tv.setTextSize(11);
tv.setTextAppearance(getApplicationContext(), R.style.BlackShadow);

风格:

<style name="BlackShadow">  
    <item name="android:shadowColor">#ff000000</item>
    <item name="android:shadowRadius">1</item>
    <item name="android:shadowDx">-1</item>
    <item name="android:shadowDy">-1</item>
    <item name="android:textSize">26dip</item>
</style>

我做错了什么?

【问题讨论】:

标签: android textview shadow


【解决方案1】:

试试这个:

tv.setShadowLayer(1.5f, -1, 1, Color.LTGRAY);

来自文档

setShadowLayer(float radius, float dx, float dy, int shadowColor)

这会在主层下方绘制一个阴影层,具有指定的偏移量和颜色以及模糊半径。

更多信息请查看http://developer.android.com/reference/android/graphics/Paint.html#setShadowLayer%28float,%20float,%20float,%20int%29

【讨论】:

  • 为什么是-1.5f?对我来说,这看起来不像是错字
  • @Alpaslan 这些值仅作为示例,根据您的要求进行更改。
猜你喜欢
  • 1970-01-01
  • 2011-03-09
  • 2011-12-16
  • 2018-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-07
  • 1970-01-01
相关资源
最近更新 更多