phinex

博客园 首页 新随笔 联系 订阅 管理
   <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp">
            <ToggleButton
                android:id="@+id/toggle_StartOnBoot"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/toggle_selector"
                android:gravity="left|center_vertical"
                android:paddingLeft="14dp"
                android:paddingRight="14dp"
                android:textColor="#ffffff"
                android:textOff=""
                android:textOn="" />
            <ImageButton
                android:id="@+id/toggleButton_StartOnBoot"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignRight="@+id/toggle_StartOnBoot"
                android:background="#00000000"
                android:src="@drawable/progress_thumb_selector" />
        </RelativeLayout>
View Code

首先是布局。

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

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

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

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

</selector>
View Code

Activity界面添加控制调用不同的资源。

toggle_StartOnBoot.SetOnClickListener (new clickToToggleListener());
    //监听事件继承
        public class clickToToggleListener : Java.Lang.Object, View.IOnClickListener  
        {  
            public void OnClick(View v)  
            {  

                ToggleButton vtoggle_StartOnBoot=v.FindViewById<ToggleButton>(Resource.Id.toggle_StartOnBoot);
                bool starOn = vtoggle_StartOnBoot.Checked;
                ChangeToggle (starOn);

            }  
        } 
public static void ChangeToggle(bool starOn)
        {


            RelativeLayout.LayoutParams params3 = (RelativeLayout.LayoutParams)toggleButton_StartOnBoot.LayoutParameters;
            if (starOn) {
                toggle_StartOnBoot.Checked = true;
                params3.AddRule(LayoutRules.AlignRight, -1);
                params3.AddRule(LayoutRules.AlignLeft, Resource.Id.toggle_StartOnBoot);
                toggleButton_StartOnBoot.LayoutParameters=params3;
                toggleButton_StartOnBoot.SetImageResource (Resource.Drawable.progress_thumb_selector);

            } else {
                toggle_StartOnBoot.Checked = false;
                params3.AddRule(LayoutRules.AlignRight, Resource.Id.toggle_StartOnBoot);
                params3.AddRule(LayoutRules.AlignLeft, -1);
                toggleButton_StartOnBoot.LayoutParameters=params3;
                toggleButton_StartOnBoot.SetImageResource (Resource.Drawable.progress_thumb_off_selector);

            }
        }

posted on 2014-07-15 09:18  phinex  阅读(819)  评论(2编辑  收藏  举报