大龙的博客

常用链接

统计

最新评论

修改Button样式 --- 转

Android SDK doc : http://androidappdocs.appspot.com/reference/android/widget/ImageButton.html

Displays a button with an image (instead of text) that can be pressed or clicked by the user. By default, an ImageButton looks like a regular Button, with the standard button background that changes color during different button states. The image on the surface of the button is defined either by the android:src attribute in the<ImageButton> XML element or by the setImageResource(int) method.

ImageButton中修改按钮样式需要定一个Xml元素表,并在Layout xml中用android:src加载效果。

To remove the standard button background image, define your own background image or set the background color to be transparent.

移除标准按钮背景图片,自定义背景图片或者设置背景图片的颜色为透明。

To indicate the different button states (focused, selected, etc.), you can define a different image for each state. E.g., a blue image by default, an orange one for when focused, and a yellow one for when pressed. An easy way to do this is with an XML drawable "selector." For example:

为了区分按钮的不同状态(聚焦,被选中等),你可以使用不同的图片....

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <item android:state_pressed="true"  
  4.           android:drawable="@drawable/button_pressed" /> <!-- pressed -->  
  5.     <item android:state_focused="true"  
  6.           android:drawable="@drawable/button_focused" /> <!-- focused -->  
  7.     <item android:drawable="@drawable/button_normal" /> <!-- default -->  
  8. </selector>  

Save the XML file in your project res/drawable/ folder and then reference it as a drawable for the source of your ImageButton (in the android:src attribute). Android will automatically change the image based on the state of the button and the corresponding images defined in the XML.

将该文件保存在 res/drawable/下 并使用android:src标签引用。 Android会自动改变对应状态的背景图片。

The order of the <item> elements is important because they are evaluated in order. This is why the "normal" button image comes last, because it will only be applied afterandroid:state_pressed and android:state_focused have both evaluated false.

在<item>标签列表的顺序是至关重要的,因为只有在android:state_pressed和android:state_focuesd 同时为false的情况下,默认的图标样式才会被显示(没按放在那里的时候,看到的按钮样式)

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:background="#ffffffff"  
  7.     >  
  8. <Button    
  9.     android:id ="@+id/btn"  
  10.     android:layout_width="wrap_content"   
  11.     android:layout_height="wrap_content"   
  12.     android:text="Hello"  
  13.     android:textColor="#ffffffff"  
  14.     android:background="@drawable/newbtn"  
  15.     />  
  16. </LinearLayout>  

posted on 2010-06-15 16:17 大龙 阅读(1707) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理