textview動態調整背景顏色background

有時候希望textview press下去之後,能夠動態的調整其背景顏色或者效果,像listview中的條目被按中之後的藍光效果,以下是通過xml來實現這個功能,

1,res/drawable下建立一個textview_style.xml文件用來定義textview的兩種風格

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">            
    <item android:state_pressed="true" android:drawable="@color/blue" />    
    <item  android:drawable="@color/transparent_background" />   

</selector>

2,res/values下建立colcors.xml文件,定義color的值,
<resources>
    <color name="transparent_background">#ffffffff</color>
    <color name="blue">#ff87cefa</color>

</resources>

3,定義textview,其中background使用定義的style

<TextView
        android:id="@+id/new_playlist"
        android:layout_width="fill_parent"
        android:layout_height="100px"
        android:textColor="#2b2b2b"
        android:textSize="32px"
        android:textStyle="bold"       
        android:text="@string/new_playlist"
        android:singleLine="true"
        android:gravity="center_vertical"

        android:background="@drawable/textview_style"
        />

以上就已經實現了textview動態效果的功能
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章