---
title: "(转)android button 效果设计"
author: "zkh43javaeye"
date: "2011-04-26T15:04:36Z"
tags: []
categories: ["非技术"]
slug: "android-button"
draft: false
lastmod: "2011-04-26T15:04:36Z"
id: 1018544
url: "https://zkh43javaeye.iteye.com/blog/1018544"
---

原址：http://ssd910.blog.163.com/blog/static/23876797201021610282142/

 

Button 有按下效果

 

\[功能]

让Button 有按下效果 更有视觉效果

 

\[代码]

1\. 先准备2张\*.png 一张供默认使用 另一张供按下使用 本例为：

 

Java代码

 

1. play.png
2. play\_down.png

play.pngplay\_down.png

 

 

2.  根据各种状态 定制化所显示的 \*.png 命名为： myselection.xml

Java代码

 

01. &lt;?xml version="1.0" encoding="utf-8"?&gt;
02. &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;
03. &lt;item
04. android:state\_pressed="false"
05. android:drawable="@drawable/play" /&gt;
06. &lt;item
07. android:state\_pressed="true"
08. android:drawable="@drawable/play\_down" /&gt;
09. &lt;item
10. android:drawable="@drawable/play" /&gt;
11. &lt;/selector&gt;

&lt;?xml version="1.0" encoding="utf-8"?&gt;&lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:state\_pressed="false" android:drawable="@drawable/play" /&gt; &lt;item android:state\_pressed="true" android:drawable="@drawable/play\_down" /&gt; &lt;item android:drawable="@drawable/play" /&gt;&lt;/selector&gt;

 

 

3\. 在 main.xml 布局中 添加Button 元件 并 设置 使用 myselection.xml

Java代码

 

01. &lt;?xml version="1.0" encoding="utf-8"?&gt;
02. &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03. android:orientation="vertical"
04. android:layout\_width="fill\_parent"
05. android:layout\_height="fill\_parent"
06. &gt;
07. &lt;TextView
08. android:layout\_width="fill\_parent"
09. android:layout\_height="wrap\_content"
10. android:text="Button Style!"
11. /&gt;
12. &lt;ImageButton
13. android:id="@+id/playorpause"
14. android:layout\_width="wrap\_content"
15. android:layout\_height="wrap\_content"
16. android:src="@xml/myselection"
17. android:background="#00000000" /&gt;
18. &lt;/LinearLayout&gt;

&lt;?xml version="1.0" encoding="utf-8"?&gt;&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout\_width="fill\_parent" android:layout\_height="fill\_parent" &gt;&lt;TextView android:layout\_width="fill\_parent" android:layout\_height="wrap\_content" android:text="Button Style!" /&gt;&lt;ImageButton android:id="@+id/playorpause" android:layout\_width="wrap\_content" android:layout\_height="wrap\_content" android:src="@xml/myselection" android:background="#00000000" /&gt;&lt;/LinearLayout&gt;

 

 

 

4\. 大家可以自己看看效果 因为不好截图

 

 

其实 除了上面的方法 还有一个方法 为：

1\. 在 maun.xml 中添加 ImageButton 且不设置使用的\*.png

Java代码

 

01. &lt;?xml version="1.0" encoding="utf-8"?&gt;
02. &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03. android:orientation="vertical"
04. android:layout\_width="fill\_parent"
05. android:layout\_height="fill\_parent"
06. &gt;
07. &lt;ImageButton
08. android:id="@+id/button"
09. android:layout\_width="wrap\_content"
10. android:layout\_height="wrap\_content"
11. /&gt;
12. &lt;/LinearLayout&gt;

&lt;?xml version="1.0" encoding="utf-8"?&gt;&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout\_width="fill\_parent" android:layout\_height="fill\_parent" &gt;&lt;ImageButton android:id="@+id/button" android:layout\_width="wrap\_content" android:layout\_height="wrap\_content" /&gt;&lt;/LinearLayout&gt;

 

 

2\. 在该ImageButton上设置监听器 并根据其状态使用对应的资源 但是必须要设置默认资源

Java代码

 

01. ImageButton btn = (ImageButton) findViewById(R.id.button);
02. //to set its default \*.png
03. btn.setBackgroundResource(R.drawable.play);
04. btn.setOnTouchListener(new ImageButton.OnTouchListener(){
05. @Override
06. public boolean onTouch(View arg0, MotionEvent arg1) {
07. // TODO Auto-generated method stub
08. if(arg1.getAction() == MotionEvent.ACTION\_DOWN){
09. arg0.setBackgroundResource(R.drawable.play\_down);
10. }
11. else if(arg1.getAction() == MotionEvent.ACTION\_UP){
12. arg0.setBackgroundResource(R.drawable.play);
13. }
14. return false;
15. }
16. });

ImageButton btn = (ImageButton) findViewById(R.id.button); //to set its default \*.png btn.setBackgroundResource(R.drawable.play); btn.setOnTouchListener(new ImageButton.OnTouchListener(){ @Override public boolean onTouch(View arg0, MotionEvent arg1) { // TODO Auto-generated method stub if(arg1.getAction() == MotionEvent.ACTION\_DOWN){ arg0.setBackgroundResource(R.drawable.play\_down); } else if(arg1.getAction() == MotionEvent.ACTION\_UP){ arg0.setBackgroundResource(R.drawable.play); } return false; } });

 

 

 

具体哪个方法更好 应该根据自己的场合：

1\. 只有一个Button 推荐使用第一个方法

2\. 有几个Button 推荐使用第二个 统一定义 然后根据指定的id 来使用目标\*.png