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();
注:此源码摘自环信
时间: 2024-11-05 18:50:43