日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android自制SwitchBar(附资源)

發(fā)布時間:2025/4/16 Android 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android自制SwitchBar(附资源) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

折騰了好幾天,這個App基本上是完成了,其中有一部分是自定義一個SwitchBar,網(wǎng)上看了好多別人的解決辦法,都是基于系統(tǒng)自帶的SwitchBar,只是修改了一些背景圖片資源什么的,效果并不是很理想,以下是我的解決辦法,先貼效果圖:


首先是layout中的switchbar.xml文件:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><FrameLayoutandroid:id="@+id/speakerframe"android:layout_width="296px"android:layout_height="100px"android:layout_gravity="center"android:background="@mipmap/btn_frame"android:onClick="onClick"android:layout_marginTop="30dp"android:layout_weight="1"><android.support.v7.widget.AppCompatImageViewandroid:id="@+id/bspeaker"android:layout_width="416px"android:layout_height="match_parent"android:layout_marginEnd="20dp"android:layout_gravity="center"app:srcCompat="@mipmap/btn_slider_n"android:background="@mipmap/btn_state_bspeaker_n" /></FrameLayout><FrameLayoutandroid:id="@+id/lightframe"android:layout_width="296px"android:layout_height="100px"android:layout_gravity="center"android:background="@mipmap/btn_frame"android:onClick="onClick"android:layout_marginTop="30dp"android:layout_weight="1"><android.support.v7.widget.AppCompatImageViewandroid:id="@+id/light"android:layout_width="416px"android:layout_height="match_parent"android:layout_marginEnd="20dp"android:layout_gravity="center"app:srcCompat="@mipmap/btn_slider_n"android:background="@mipmap/btn_state_light_n" /></FrameLayout><android.support.v7.widget.AppCompatImageViewandroid:id="@+id/speed"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"app:srcCompat="@mipmap/btn_speed_bg"android:layout_weight="10"/></LinearLayout>基本思想就是btn_frame作為Layout的邊框,大小寬度作為Layout的長和寬,(但是,這樣做的有明顯的缺陷,不同分辨率的手機,屏幕大小不同的手機,顯示的大小效果就不同了,后期再想解決辦法)將bspeaker作為SpeakerFrame的資源文件,其中bspeaker是由btn_state_bspeaker_n和btn_slider組成的,由于bspeaker的長度大于SpeakerFrame的長度,故表現(xiàn)出的效果就是SwitchBar。(Speaker和Lighjt完全同理,故只解釋Speaker)

簡言之,和組成,當點擊事件發(fā)生時,改變bspeaker的位置即可。(忽略我截圖的大小…)

于是剩下的就是點擊事件發(fā)生的方法了:

@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.speakerframe:onspeak();break;case R.id.lightframe:onLight();break;default:break;}}int set = 1;private void onLight() {Toast.makeText(this, "點擊燈光", Toast.LENGTH_SHORT).show();ImageView lightBar = (ImageView) findViewById(light);FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) lightBar.getLayoutParams();if (set == 1) {params.setMarginStart(120);//其余的相關操作……set = 0;} else {params.setMarginStart(0);set = 1;}lightBar.setLayoutParams(params);}private void onspeak() {Toast.makeText(this, "點擊聲音", Toast.LENGTH_SHORT).show();ImageView speakBar = (ImageView) findViewById(bspeaker);FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) speakBar.getLayoutParams();if (set == 1) {params.setMarginStart(120);//其余的相關操作……set = 0;} else {params.setMarginStart(0);set = 1;}speakBar.setLayoutParams(params);}
至此,便完成了完全自定義的“SwitchBar”,如果有老鐵需要用到拿去用即可,不過,資源只是聲音和燈光,如需別的功能,自行P掉即可。另外,這個解決辦法其中有很大的問題,就是對于bSpeaker的移動大小,是對我測試的手機適用,若換個尺寸不同或者像素不同的手機,那就會出現(xiàn)偏差,由于只是自己做著玩而不是正規(guī)的使用,所以就暫且得過且過了,如果哪位大神有兼容性好的辦法,煩請不吝賜教!


總結

以上是生活随笔為你收集整理的Android自制SwitchBar(附资源)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。