短視訊系統原始碼,實現按鈕開啟關閉,顏色可自定義

zhibo系統開發發表於2021-12-23

短視訊系統原始碼,實現按鈕開啟關閉,顏色可自定義

 <Switch
            android:id="@+id/switch_shield_msg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="15dp"
            android:thumb="@drawable/chat_more_switch_selector"
            android:track="@drawable/chat_more_switch_nomal" />


chat_more_switch_selector主要用來實現中間的圓

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="
    <item android:state_checked="true">
        <shape android:shape="oval">
            <!--            圓的寬高-->
            <size android:width="16dp" android:height="16dp" />
            <!--            選中之後顏色-->
            <solid android:color="#C5C5C5" />
        </shape>
    </item>
    <item>
        <shape android:shape="oval">
          <!--            圓的寬高-->
            <size android:width="16dp" android:height="16dp" />
            <!--            未選中預設顏色-->
            <solid android:color="#C5C5C5" />
        </shape>
    </item>
</selector>

chat_more_switch_nomal實現長方形圓

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="
    <item android:state_checked="true">
        <shape android:shape="rectangle">
          <!--            長圓寬高-->
            <size android:width="32dp" android:height="16dp" />
            <!--            選中之後顏色白色-->
            <solid android:color="@color/main_bg" />
            <corners android:radius="8dp" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
          <!--            長圓寬高-->
            <size android:width="32dp" android:height="16dp" />
            <!--            未選中預設顏色是灰色-->
            <solid android:color="#6D6D6D" />
            <corners android:radius="8dp" />
        </shape>
    </item>
</selector>


監聽選中未選中

 switch_shield_msg.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){
               }else{
              }
            }
        });


以上就是短視訊系統原始碼,實現按鈕開啟關閉,顏色可自定義實現的相關程式碼, 更多內容歡迎關注之後的文章


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69978258/viewspace-2849064/,如需轉載,請註明出處,否則將追究法律責任。

相關文章