關於切換自定義組件(Button,CheckBox,EditText)被點擊或選中時背景的方法


當我們覺得某些組件的樣式 不給力的時候(比如Button,CheckBox,EditText),就會想到給這些組件自定義樣式了,這裏提供簡單的兩種方法:

一、改背景
比如說Button,我們可以使用ImageButton代替,然後設置它點擊時的效果,比如發光啥的
又比如說CheckBox,我們可以自由設置它被選中和未選中時的樣式。圓形、方形、三角形 隨意了。。
然後是EditText,同樣道理。

下面是方法:

首先新建一個xml文件,如下所示:(這是個處理不同動作下,組件需要轉換的背景文件)

1、ImageButton背景的切換方法

-----------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_focused="true"
android:state_pressed="false"
android:drawable="@drawable/imageview_normal" /> <!--未點擊 的時候ImageView的背景圖-->

<item android:state_focused="true"
android:state_pressed="true"
android:drawable="@drawable/imageview_pressed" /> <!--點擊 的時候ImageView的背景圖-->

<item android:state_focused="false"
android:state_pressed="true"
android:drawable="@drawable/imageview_pressed" /> <!--點擊 的時候ImageView的背景圖 -->


<item android:drawable="@drawable/imageview_normal" /> <!-- 未點擊 的時候ImageView的背景圖 -->
</selector>
-----------------------------------------------

使用方法,xml相對應的ImageButton設置屬性 android:background="@drawable/上面文件的文件名" ,就可以了


2、CheckBox背景的切換方法

-----------------------------------------------
<?xml version="1.0" encoding="utf-8"?>

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

<item android:state_checked="true"
android:drawable="@drawable/checkbox_selected" /><!--選中時效果圖-->

<item android:state_checked="false"
android:drawable="@drawable/checkbox_unselected" /><!--未選中時效果圖-->

</selector>
-----------------------------------------------

使用方法,xml相對應的ImageButton設置屬性 android:button="@drawable/上面文件的文件名" ,就可以了

3、EditText背景切換的方法

-----------------------------------------------
<?xml version="1.0" encoding="utf-8"?>

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

<item android:state_focused="true"
android:drawable="@drawable/edittext_selected" /><!--選中時效果圖-->

<item android:state_focused="false"
android:drawable="@drawable/edittext_unselected" /><!--未選中時效果圖-->

</selector>
-----------------------------------------------

使用方法,xml相對應的ImageButton設置屬性 android:background="@drawable/上面文件的文件名" ,就可以了
注:有時編輯框中的文字會在背景圖的外面,比如文字在圖片所包圍的區域左邊,這個時候 可以使用 諸如 android:pandingLefg="10dip"的方式,跳轉文本的位置


二、設置style
以EditText爲例,
首先在res/values目錄下新建一個styles.xml的文件,

-----------------------------------------------
<?xml version="1.0" encoding="utf-8"?>

<resources>

<style name="my_edittext_style" parent="@android:style/Widget.EditText">

<item name="android:background">@drawable/edittext_selector</item>

</style>

</resources>
---------------------------------------------

註解:my_edittext_style是這個樣式的名稱 , parent="@android:style/Widget.EditText" 是這個樣式所適用的組件名

<item name="android:background">@drawable/edittext_selector</item> 說明這個樣式 是一個背景樣式,引用的drawable資源是edittext_selector,恩。這個東西就是我們上面所寫的xml文件。。


接着是使用方法:

------------------------------------------------

<EditText

style="@style/my_edittext_style"

android:text="My EditText"

android:layout_width="fill_parent"

android:layout_height="wrap_content">

</EditText>

--------------------------------------------

效果圖:

正常情況。

操作後,



發佈了6 篇原創文章 · 獲贊 0 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章