Giter Site home page Giter Site logo

switchbutton's Introduction

SwitchButton

a simple slide layout library,just one java class file! 自定义控件,一个类的事

先来张图找找感觉

特点说明:

  • Just one class。自定义控件SwitchButtonView,属性配置attrs.xml。
  • 支持点击 或者 滑动切换状态。
  • 可以设定switchbutton大小,设置layout_width属性值即可,程序按比例计算出layout_height值。
  • 如图,分别是指定android:layout_width="200dp"和android:layout_width="wrap_content"效果图。
  • 提供可配置各种想要的颜色接口。

attrs.xml文件:

<declare-styleable name="SwitchButtonView">
    <!--圆角矩形与内圆的间距-->
    <attr name="innerPaddingWidth" format="dimension|reference" />
    <!--底纹颜色-->
    <attr name="bgColor1" format="color|reference" />
    <!--填充颜色-->
    <attr name="bgColor2" format="color|reference" />
    <!--边框粗细-->
    <attr name="bgWidth" format="dimension|reference" />
    <!--选中颜色-->
    <attr name="coverColor" format="color|reference" />
    <!--圆默认颜色-->
    <attr name="circleDefaultColor" format="color|reference" />
    <!--圆按下颜色-->
    <attr name="circleSelectColor" format="color|reference" />
    <!--是否打开-->
    <attr name="isToggleOn" format="boolean" />
</declare-styleable>

###layout.xml文件 <RelativeLayout xmlns:dk="http://schemas.android.com/apk/res-auto" ...>

    <com.duke.switchbutton_test.SwitchButtonView
        android:id="@+id/myswitchbutton"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        dk:isToggleOn="true" />

    <com.duke.switchbutton_test.SwitchButtonView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/myswitchbutton"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        dk:isToggleOn="true" />
</RelativeLayout>

###actiivity.java代码:

public class MainActivity extends AppCompatActivity {
    private SwitchButtonView myswitchbutton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myswitchbutton = (SwitchButtonView) findViewById(R.id.myswitchbutton);
        myswitchbutton.setOnToggleChangeListener(new SwitchButtonView.OnToggleChangeListener() {
            @Override
            public void onChange(boolean isToggleOn) {
                ...
            }
        });
        myswitchbutton.setIsToggleOn(false);代码设置状态
    }
}

   都是自定义控件那点事,没得多说的。
更多资料请关注我的博客

switchbutton's People

Contributors

mengzhinan avatar

Stargazers

 avatar Jerry avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.