【问题标题】:how to set text size using the selector?如何使用选择器设置文本大小?
【发布时间】:2012-09-09 01:03:09
【问题描述】:

我使用选择器,但我不知道如何设置文本大小。也许我做错了什么 - 帮助

箭头.xml

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

    <item android:drawable="@drawable/button_pressed" android:state_pressed="true" android:color="@color/green"></item>

</selector>

【问题讨论】:

  • 我和你有同样的问题,但仍然找不到答案:)

标签: android


【解决方案1】:

您可以在动画资源下使用选择器:

<!-- res/animator/text_size.xml -->
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true">
        <set>
            <objectAnimator
                android:duration="0"
                android:propertyName="textSize"
                android:valueTo="18sp"
                android:valueType="floatType"/>
        </set>
    </item>
    <item android:state_focused="false">
        <set>
            <objectAnimator
                android:duration="0"
                android:propertyName="textSize"
                android:valueTo="10sp"
                android:valueType="floatType"/>
        </set>
    </item>
</selector>

您可以将持续时间设为 0 以直接跳转到该值,也可以在其中设置持续时间。

然后在你的视图上设置android:stateListAnimator

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 3"
    android:stateListAnimator="@animator/text_size"/>

【讨论】:

  • 这很有用,但是我发现在 objectAnimator valueTo/From 中使用 sp 值会导致它两次转换为浮点数,因为 TextView.setTextSize(float) 将值视为 sp。不指定单位使文本大小与其余资源保持一致。
  • 我不关注,也许写一个答案。
  • 也许这在 2017 年有效,但在 2020 年不再有效,它说 &lt;objectAnimator&gt; is not allowed here 并且自动完成功能已关闭。应用程序运行,但动画师被忽略。
【解决方案2】:

创建您自己的style.xml 并定义文本样式。然后在选择器中设置样式。

style.xml

<style name="myStyle">  
    <item name="android:textSize">9px</item>
    <item name="android:textColor">#fff</item>
    <item name="android:textStyle">bold</item>
</style>

在你的选择器中

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          style="@style/myStyle" /> 
</selector>

【讨论】:

  • 嗨@Apurva,将选择器文件放在哪里?它在可绘制对象中吗?
  • 这真的有效吗?我收到警告“未知属性样式”。 AFAIK 选择器仅适用于可绘制对象而不适用于样式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-19
  • 2013-09-22
  • 2020-06-13
  • 1970-01-01
  • 1970-01-01
  • 2010-09-09
  • 2012-07-28
相关资源
最近更新 更多