小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

android 中ImageButton按下改變背景圖片的效果

 風(fēng)雪夜歸人_95 2014-03-13
最近在做一個app的登陸界面,才發(fā)現(xiàn)原來認為很簡單的UI效果,其實背后卻蘊含的知識很多,積累一個算一個吧。
實現(xiàn)方法有兩種:一種是添加代碼,一種是配置xml文件。
方法一:代碼添加

ImageButton btn = (ImageButton)findViewById(R.id.imageButton1);

btn.setOnTouchListener(new View.OnTouchListener(){

public boolean onTouch(View v, MotionEvent event) {

if(event.getAction() == MotionEvent.ACTION_DOWN){

//重新設(shè)置按下時的背景圖片

((ImageButton)v).setImageDrawable(getResources().getDrawable(R.drawable.android_btn_pressed));

}else if(event.getAction() == MotionEvent.ACTION_UP){

//再修改為抬起時的正常圖片

((ImageButton)v).setImageDrawable(getResources().getDrawable(R.drawable.android_btn));

}

return false;

}

});

 方法2:配置xml文件
步驟1:在Layout下增加一個image_btn_press.xml文件

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

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

<item android:state_pressed="false" android:drawable="@drawable/android_btn" />

<item android:state_focused="true" android:drawable="@drawable/android_btn" />

<item android:state_pressed="true" android:drawable="@drawable/android_btn_pressed" />

</selector>

步驟2:在main.xml中設(shè)置圖片按鈕的屬性,裝上面的xml文件增加到圖片按鈕中

<ImageButton

android:id="@+id/imageButton2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@layout/image_btn_press" />

 需要特別注意的是在ImageButton中,如果使用XML配置文件來設(shè)置圖片的效果的話,就不要再指定它的android:src=""屬性值了,否則圖片的按下效果就出不來了。
 

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多