zoukankan      html  css  js  c++  java
  • 自定义开关按钮

    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();
    注:此源码摘自环信

  • 相关阅读:
    爬虫笔记:使用python生成词云(八)
    31丨2内核剖析
    六飞翔篇(4讲)30 丨 2特性概览
    29 丨 我应该迁移到HTTPS吗?
    28 丨 连接太慢该怎么办:HTTPS的优化
    27丨更好更快的握手:TLS1.3特性解析
    26丨信任始于握手:TLS1.2连接过程解析
    Python全栈工程师 (exercises)
    Python全栈工程师(每周总结:2)
    Python全栈工程师(函数嵌套、变量作用域)
  • 原文地址:https://www.cnblogs.com/shixm/p/5430413.html
Copyright © 2011-2022 走看看