Android成长之路-Button、ImageButton、ToggleButton按钮的功能和用法

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

定义一个layout布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ToggleButton
        android:id="@+id/toggle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:textOn="纵向排列"
        android:textOff="横向排列" />
    <!-- android:checked  如果是true,运行时显示的是 android:textOn 的值
                          如果是false,运行是显示的是 android:textOff 的值
                                  
            所以:
            1.如果checked=true 且 textOn=纵向排列
                 那么  下面的orientation="vertical"
              如果checked=false 
                 那么  下面的orientation="horizontal"
            2.如果checked=true 且 textOn=横向排列
                 那么  下面的orientation="horizontal"
              如果checked=false 
                 那么  下面的orientation="vertical"
                                               
                                               
      		就像语文课文中说的:上下文要匹配
     -->

    <LinearLayout
        android:id="@+id/test"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮一" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮二" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮三" />
    </LinearLayout>

</LinearLayout>


 

 

写一个activity文件实现功能:

package cn.csdn.activity;

import android.app.Activity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.ToggleButton;

public class ToggleButtonActivity extends Activity{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.state_layout);
		ToggleButton toggle = (ToggleButton) this.findViewById(R.id.toggle);
		final LinearLayout test = (LinearLayout) this.findViewById(R.id.test);
		toggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				/**传一个布尔型的值,来判断是true还是false,然后区分是纵向还是横向
				 * ToggleButton按钮是在不断的按下时true和false相互切换的,也就是xml定义时的checked的值*/
				if(isChecked){//true时
					test.setOrientation(1);// 1 的话,设置为纵向
				}else{//false时
					test.setOrientation(0);// 0 的话,设置为横向
				}
			}
		});
	}

	
}


 

效果图:

 

点击按钮切换布局

 


 

 

Button和ImageButton按钮的用法:

定义一个布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <!-- 普通文字按钮 -->
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="普通文字按钮"
        />
    
    <!-- 普通图片按钮 -->
    <ImageButton 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/play"
        />
    
    <!-- 带有图片的文字按钮 -->
    <Button 
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/stop"
        android:text="图片文字按钮"
        />
    <!-- 按下时显示不同图片的按钮 
         android:src="@drawable/button_selector" 引用一个Drawable资源
    -->
    <ImageButton 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/button_selector"
        />
    

</LinearLayout>


 

 

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

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

    <!-- 指定按钮按下时候显示的图片 -->
    <item
    	android:state_pressed="true"
    	android:drawable="@drawable/pic"
    />
    
    <!-- 指定按钮松开时候显示的图片 -->
    <item
    	android:state_pressed="false"
    	android:drawable="@drawable/ic_launcher"
    />

</selector>


效果图:

 

 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值