Android開發-Switch開關控件的使用
switch在佈局中的屬性
瞭解如何使用switch控件,必須從瞭解switch屬性開始,如下便是:
屬性名 | 作用 |
---|---|
textOn | 控件打開時顯示的文字 |
textOff | 控件關閉時顯示的文字 |
thumb | 控件開關的圖片 |
track | 控件開關的軌跡圖片 |
typeface | 設置字體類型 |
switchMinWidth | 開關最小寬度 |
switchPadding | 設置開關與文字的空白距離 |
switchTextAppearance | 設置文本的風格 |
checked | 設置初始選中狀態 |
splitTrack | 是否設置一個間隙,讓滑塊與底部圖片分隔(API 21及以上) |
showText | 設置是否顯示開關上的文字(API 21及以上) |
如何自定義Switch控件
在日常開發中,我們基本上不會使用最開始的Switch控件,所以就需要自定義一下這個控件,最主要設置thumb和track這兩個屬性。
選擇器 track.xml 用於控制Switch不同狀態下,滑動條的底圖
<?xml version="1.0" encoding="utf-8"?>
<!-- 底層下滑條的樣式選擇器,可控制Switch在不同狀態下,底下下滑條的顏色 -->
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="true" android:drawable="@drawable/green_track" />
<item android:drawable="@drawable/gray_track" />
</selector>
連接green_track(1)和gray_track(2)兩個xml文件
<!--1 -->
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- 高度30 此處設置寬度無效-->
<size android:height="30dp"/>
<!-- 圓角弧度 15 -->
<corners android:radius="15dp"/>
<!-- 變化率 定義從左到右的顏色不變 -->
<gradient
android:endColor="#888888"
android:startColor="#888888" />
</shape>
<!--2 -->
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- 高度40 -->
<size android:height="40dp" android:width="40dp"/>
<!-- 圓角弧度 20 -->
<corners android:radius="20dp"/>
<!-- 變化率 -->
<gradient
android:endColor="#eeeeee"
android:startColor="#eeeeee" />
<stroke android:width="1dp"
android:color="#666666"/>
</shape>
選擇器 thumb.xml 用於控制Switch不同狀態下,按鈕的顯示狀態
<?xml version="1.0" encoding="utf-8"?>
<!-- 按鈕的選擇器,可以設置按鈕在不同狀態下的時候,按鈕不同的顏色 -->
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="true" android:drawable="@drawable/green_thumb" />
<item android:drawable="@drawable/gray_thumb" />
</selector>
連接green_thumb(3)和gray_thumb(4)兩個xml文件
<!--3 -->
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- 高度40 -->
<size android:height="40dp" android:width="40dp"/>
<!-- 圓角弧度 20 -->
<corners android:radius="20dp"/>
<!-- 變化率 -->
<gradient
android:endColor="#eeeeee"
android:startColor="#eeeeee" />
<stroke android:width="1dp"
android:color="#33da33"/>
</shape>
<!--4 -->
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- 高度40 -->
<size android:height="40dp" android:width="40dp"/>
<!-- 圓角弧度 20 -->
<corners android:radius="20dp"/>
<!-- 變化率 -->
<gradient
android:endColor="#eeeeee"
android:startColor="#eeeeee" />
<stroke android:width="1dp"
android:color="#666666"/>
</shape>
最後在activity_main.xml文件中添加這個控件,設置thumb和track這兩個屬性
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:gravity="center"
xmlns:android="http://schemas.android.com/apk/res/android" >
<Switch
android:id="@+id/kaiguan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:thumb="@drawable/thumb"
android:track="@drawable/track"
/>
</LinearLayout>
Switch在Java文件中實現開關事件
MainActivity.java文件中設置Switch控件監聽,調用setOnCheckedChangeListener() 方法實現開關事件
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Switch s;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
s = findViewById(R.id.kaiguan);
s.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (s.isChecked()){
Toast.makeText(MainActivity.this,"開啓",Toast.LENGTH_LONG).show();
}else {
Toast.makeText(MainActivity.this,"關閉",Toast.LENGTH_LONG).show();
}
}
});
}
}
效果圖
借鑑所得: https://blog.csdn.net/yijiaodingqiankun/article/details/83306957