码迷,mamicode.com
首页 > 其他好文 > 详细

自定义开关按钮

时间:2016-04-25 13:01:52      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

attrs.xml
<declare-styleable name="EaseSwitchButton">
<attr name="switchOpenImage" format="reference"/>
<attr name="switchCloseImage" format="reference"/>
<attr name="switchStatus">
<enum name="open" value="0"/>
<enum name="close" value="1"/>
</attr>
</declare-styleable>

EaseSwitchButton.java
package com.easemob.easeui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.easemob.easeui.R;

public class EaseSwitchButton extends FrameLayout{

private ImageView openImage;
private ImageView closeImage;

public EaseSwitchButton(Context context, AttributeSet attrs, int defStyle) {
this(context, attrs);
}

public EaseSwitchButton(Context context) {
this(context, null);
}

public EaseSwitchButton(Context context, AttributeSet attrs) {
super(context, attrs);

TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.EaseSwitchButton);
Drawable openDrawable = ta.getDrawable(R.styleable.EaseSwitchButton_switchOpenImage);
Drawable closeDrawable = ta.getDrawable(R.styleable.EaseSwitchButton_switchCloseImage);
int switchStatus = ta.getInt(R.styleable.EaseSwitchButton_switchStatus, 0);
ta.recycle();

LayoutInflater.from(context).inflate(R.layout.ease_widget_switch_button, this);
openImage = (ImageView) findViewById(R.id.iv_switch_open);
closeImage = (ImageView) findViewById(R.id.iv_switch_close);
if(openDrawable != null){
openImage.setImageDrawable(openDrawable);
}
if(closeDrawable != null){
closeImage.setImageDrawable(closeDrawable);
}
if(switchStatus == 1){
closeSwitch();
}

}

/**
* 开关是否为打开状态
*/
public boolean isSwitchOpen(){
return openImage.getVisibility() == View.VISIBLE;
}

/**
* 打开开关
*/
public void openSwitch(){
openImage.setVisibility(View.VISIBLE);
closeImage.setVisibility(View.INVISIBLE);
}

/**
* 关闭开关
*/
public void closeSwitch(){
openImage.setVisibility(View.INVISIBLE);
closeImage.setVisibility(View.VISIBLE);
}
}


ease_widget_switch_button.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<ImageView
android:id="@+id/iv_switch_open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ease_open_icon"
android:visibility="visible" />

<ImageView
android:id="@+id/iv_switch_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ease_close_icon"
android:visibility="invisible" />

</FrameLayout>

技术分享

技术分享


引用:
<com.easemob.easeui.widget.EaseSwitchButton
android:id="@+id/switch_notification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />

notifiSwitch.closeSwitch();
notifiSwitch.openSwich();
注:此源码摘自环信

自定义开关按钮

标签:

原文地址:http://www.cnblogs.com/shixm/p/5430413.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!