2012-09-16 6 views
5

セレクタを使用していますが、テキストサイズを設定する方法がわかりません。たぶん私は何か間違ったことをやっている -セレクタを使ってテキストサイズを設定する方法は?

<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> 
+0

を、私はあなたと同じ質問を持っているが、まだ答え:) –

答えて

-1

arrow.xml

を助ける独自の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> 
+0

こんにちは@Apurva、セレクタファイルを置くために見つけることができませんか?それはドロアブルの中にありますか? – Elye

+0

もちろん可能です。 – Apurva

+3

これは本当に機能しますか?私は "Unknown attribute style"と警告します。 AFAIKセレクタは、スタイルではなく、ドロウアブルでのみ動作します。 – kalehv

2

にあなたがアニメーター資源の下でセレクタを使用することができます。

<!-- 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"/> 
+0

これは便利ですが、objectAnimatorの 'valueTo/From'でsp値を使用すると、' TextView.setTextSize(float) 'が値をspとして扱うため、floatに2回変換されることがわかりました。ユニットを指定しないと、残りのリソースとテキストサイズが一致したままになります。 – Zharf

+0

私は追いついていない、答えとして書くかもしれない。 – weston

関連する問題