ToggleButton Button ImageButton 的区别和用法

原文:http://blog.csdn.net/like7xiaoben/article/details/7208835



ToggleButton按钮的功能和用法:(实现动态控制布局)

定义一个layout布局文件:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <ToggleButton  
  8.         android:id="@+id/toggle"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:checked="true"  
  12.         android:textOn="纵向排列"  
  13.         android:textOff="横向排列" />  
  14.     <!-- android:checked  如果是true,运行时显示的是 android:textOn 的值  
  15.                           如果是false,运行是显示的是 android:textOff 的值  
  16.                                     
  17.             所以:  
  18.             1.如果checked=true 且 textOn=纵向排列  
  19.                  那么  下面的orientation="vertical"  
  20.               如果checked=false   
  21.                  那么  下面的orientation="horizontal"  
  22.             2.如果checked=true 且 textOn=横向排列  
  23.                  那么  下面的orientation="horizontal"  
  24.               如果checked=false   
  25.                  那么  下面的orientation="vertical"  
  26.                                                  
  27.                                                  
  28.             就像语文课文中说的:上下文要匹配  
  29.      -->  
  30.   
  31.     <LinearLayout  
  32.         android:id="@+id/test"  
  33.         android:layout_width="fill_parent"  
  34.         android:layout_height="fill_parent"  
  35.         android:orientation="vertical" >  
  36.   
  37.         <Button  
  38.             android:layout_width="wrap_content"  
  39.             android:layout_height="wrap_content"  
  40.             android:text="按钮一" />  
  41.   
  42.         <Button  
  43.             android:layout_width="wrap_content"  
  44.             android:layout_height="wrap_content"  
  45.             android:text="按钮二" />  
  46.   
  47.         <Button  
  48.             android:layout_width="wrap_content"  
  49.             android:layout_height="wrap_content"  
  50.             android:text="按钮三" />  
  51.     </LinearLayout>  
  52.   
  53. </LinearLayout>  


 

 

写一个activity文件实现功能:

[java]  view plain copy
  1. package cn.csdn.activity;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.widget.CompoundButton;  
  6. import android.widget.CompoundButton.OnCheckedChangeListener;  
  7. import android.widget.LinearLayout;  
  8. import android.widget.ToggleButton;  
  9.   
  10. public class ToggleButtonActivity extends Activity{  
  11.   
  12.     @Override  
  13.     protected void onCreate(Bundle savedInstanceState) {  
  14.         // TODO Auto-generated method stub  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.state_layout);  
  17.         ToggleButton toggle = (ToggleButton) this.findViewById(R.id.toggle);  
  18.         final LinearLayout test = (LinearLayout) this.findViewById(R.id.test);  
  19.         toggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {  
  20.               
  21.             @Override  
  22.             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {  
  23.                 /**传一个布尔型的值,来判断是true还是false,然后区分是纵向还是横向 
  24.                  * ToggleButton按钮是在不断的按下时true和false相互切换的,也就是xml定义时的checked的值*/  
  25.                 if(isChecked){//true时  
  26.                     test.setOrientation(1);// 1 的话,设置为纵向  
  27.                 }else{//false时  
  28.                     test.setOrientation(0);// 0 的话,设置为横向  
  29.                 }  
  30.             }  
  31.         });  
  32.     }  
  33.   
  34.       
  35. }  


 

效果图:

 

点击按钮切换布局

 


 

 

Button和ImageButton按钮的用法:

定义一个布局:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <!-- 普通文字按钮 -->  
  8.     <Button   
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="普通文字按钮"  
  12.         />  
  13.       
  14.     <!-- 普通图片按钮 -->  
  15.     <ImageButton   
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content"  
  18.         android:src="@drawable/play"  
  19.         />  
  20.       
  21.     <!-- 带有图片的文字按钮 -->  
  22.     <Button   
  23.         android:id="@+id/button"  
  24.         android:layout_width="wrap_content"  
  25.         android:layout_height="wrap_content"  
  26.         android:background="@drawable/stop"  
  27.         android:text="图片文字按钮"  
  28.         />  
  29.     <!-- 按下时显示不同图片的按钮   
  30.          android:src="@drawable/button_selector" 引用一个Drawable资源  
  31.     -->  
  32.     <ImageButton   
  33.         android:layout_width="wrap_content"  
  34.         android:layout_height="wrap_content"  
  35.         android:src="@drawable/button_selector"  
  36.         />  
  37.       
  38.   
  39. </LinearLayout>  


 

 

定义Drawable资源:(实现图片按钮按下和松开时显示不同图片)

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.   
  4.     <!-- 指定按钮按下时候显示的图片 -->  
  5.     <item  
  6.         android:state_pressed="true"  
  7.         android:drawable="@drawable/pic"  
  8.     />  
  9.       
  10.     <!-- 指定按钮松开时候显示的图片 -->  
  11.     <item  
  12.         android:state_pressed="false"  
  13.         android:drawable="@drawable/ic_launcher"  
  14.     />  
  15.   
  16. </selector>  


效果图:

 

 

下图是第四个按钮在按下时的状态:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值