2013-12-18 55 views
1

我試圖弄清楚如何在觸摸時將外部發光添加到TextView。我正在使用的方法是使用選擇器,但它似乎沒有工作。如何在TextView上使用自定義選擇器樣式

我有以下結構

佈局\ HomeView.axml

<TextView 
    android:id="@+id/textview1" 
    android:clickable="true" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:adjustViewBounds="true" 
    style="@drawable/control_selector_state" /> 

繪製\ control_selector_state.xml

<!-- yes these are all the same for testing purposes --> 

<?xml version="1.0" encoding="utf-8" ?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_focused="true" 
     style="@style/control_style_focused"/> 

    <item android:state_focused="true" 
     android:state_pressed="true" 
     style="@style/control_style_focused" /> 

    <item android:state_pressed="true" 
     style="@style/control_style_focused" /> 

    <item style="@style/control_style_focused" /> 

</selector> 

值\ styles.xml

<resources> 
    <style name="control_style_focused"> 
    <item name="android:shadowColor">#0000ff</item> 
    <item name="android:textColor">#ff0000</item> 
    <item name="android:shadowDx">0.0</item> 
    <item name="android:shadowRadius">8</item> 
    </style> 
</resources> 

我遇到的問題是我的TextView文本是白色的,而且樣式似乎沒有應用。

我該如何得到這種風格適用於我的TextView?

+0

將shadowDx設置爲0.5,看看會發生什麼。 0.0完全置於小部件之下。 –

+0

0.0的陰影是模仿發光而不是陰影。 –

回答

0

所以@Longwayto說,選擇器樣式只適用於drawables。這並不意味着這是不可能的。

這是一個工作方法。

首先創建一個擴展TextView

public class MyTextView: TextView 
{ 
    private readonly Context _context; 

    public FontIconTextView(Context context, IAttributeSet attrs) : base(context) 
    { 
     _context = context; 
     Initialize(attrs); 
    } 

    private void Initialize(IAttributeSet attrs) 
    { 
     var a = _context.Theme.ObtainStyledAttributes(attrs, Resource.Styleable.MyTextView, 0, 0); 
     _touchGlowColor = a.GetString(Resource.Styleable.MyTextView_TouchGlowColor); 
     _touchGlowSize = a.GetInteger(Resource.Styleable.MyTextView_TouchGlowSize, 0); 
     Initialize(); 
    } 

    private void Initialize() 
    { 
     // other initialize stuff happens here... 

    } 

    private int _touchGlowSize; 
    private string _touchGlowColor; 
    public override bool OnTouchEvent(MotionEvent motionEvent) 
    { 
     if (Enabled) 
     { 
      var color = string.IsNullOrEmpty(_touchGlowColor) ? new Color(255, 255, 255, 255) : Color.ParseColor(_touchGlowColor); 

      switch (motionEvent.Action) 
      { 
       case MotionEventActions.Down: 
        SetShadowLayer(_touchGlowSize, 0, 0, color); 
        break; 
       case MotionEventActions.Up: 
       case MotionEventActions.Cancel: 
        SetShadowLayer(0, 0, 0, new Color(255, 255, 255, 255)); 
        break; 

      } 
     } 
     return base.OnTouchEvent(motionEvent); 
    } 
} 

然後自定義TextView的,你必須進入你的價值觀目錄,並指定您的自定義屬性。

資源\ \值CustomBindingAttributes.xml

<?xml version="1.0" encoding="utf-8" ?> 
<resources> 
    <declare-styleable name="MyTextView"> 
    <attr name="TouchGlowColor" format="string" /> 
    <attr name="TouchGlowSize" format="integer" /> 
    </declare-styleable> 
</resources> 

上述所有將是可重複使用的整個應用程序......沒有更多的複製狗屎每個視圖。

最後,這是按鈕的外觀。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:local="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <!-- obviously the foo/bar is just to show that you add android attrs like normal --> 
    <some.name.space.MyTextView 
     android:foo="foo" 
     amdroid:bar="bar" 
     local:TouchGlowColor="#66e400" 
     local:TouchGlowSize="20" /> 

</LinearLayout> 

有一個問題我跑進是設置TouchGlowSize 30造成應用程序崩潰。不知道爲什麼atm

相關問題