Android studio 开关控件 SwitchCompat

Switch是在4.0以后推出的,要注意开发时的minsdk设置,google在API 21后也推出support v7 包下的SwitchCompa的Material Design

效果图

在这里插入图片描述

布局
 <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="20sp"
        >
        <androidx.appcompat.widget.SwitchCompat
            android:id="@+id/switchCompat"
            android:text="按钮1"
            android:checked="false"
            app:showText="true"
            android:textOn="开"
            android:textOff="关"
            app:switchPadding="20dp"
            app:switchTextAppearance="@style/ontextoff"
            app:theme="@style/scstyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <androidx.appcompat.widget.SwitchCompat
            android:layout_marginLeft="20sp"
            android:text="按钮2"
            android:checked="true"
            app:showText="true"
            android:textOn="开"
            android:textOff="关"
            app:switchPadding="20dp"
            app:switchTextAppearance="@style/ontextoff"
            app:theme="@style/scstyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>
  • @style
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <!--文字颜色和大小-->
    <style name="ontextoff" parent="Theme.AppCompat.Light">
        <item name="android:textColor">#0d09f1</item>
        <item name="android:textSize">8sp</item>
    </style>
    
    <style name="scstyle" parent="Theme.AppCompat.Light">
        <!--开启时的颜色-->
        <item name="colorControlActivated">#00e6db</item>
        <!--关闭时的颜色-->
        <item name="colorSwitchThumbNormal">#FFFFFF</item>
        <!--关闭时的轨迹颜色-->
<!--        <item name="android:colorForeground">#ecd502</item>-->
    </style>
</resources>
  • 代码设置轨道和按钮
        switchCompat.setTrackResource(R.mipmap.ic_launcher);//轨道 自定义图片
        switchCompat.setThumbResource(R.mipmap.ic_launcher);//按钮 自定义图片
  • 点击的处理
switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b){
                    Log.i("TAG","开的点击处理--------");
                }else {
                    Log.i("TAG","关的点击处理--------");
                }
            }
        });
属性
  • android:typeface=“normal”:设置字体类型
  • android:track=“”:设置开关的轨迹图片
  • android:textOff=“开”:设置开关checked的文字
  • android:textOn=“关”:设置开关关闭时的文字
  • android:thumb=“”:设置开关的图片
  • android:switchMinWidth=“”:开关最小宽度
  • android:switchPadding=“”:设置开关 与文字的空白距离
  • android:switchTextAppearance=“”:设置文本的风格
  • android:checked=“”:设置初始选中状态
  • android:splitTrack=“true”:是否设置一个间隙,让滑块与底部图片分隔(API 21及以上)
    android:showText=“true”:设置是否显示开关上的文字(API 21及以上)
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Android Studio中的表格控件是指DataGridView数据表格控件,可以通过自定义来实现。您可以使用XML布局文件来定义表格控件的外观和布局,然后在Activity中使用适配器来提供数据给表格控件。以下是一个示例代码片段,展示如何在Android Studio中实现表格控件: 引用中的XML布局文件示例: ``` <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ListView android:id="@ id/listView" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> ``` 引用中的示例代码展示了如何在Activity中使用ArrayAdapter为ListView提供数据。您可以根据自己的需求修改适配器和数据源来实现表格控件的效果。以下是一个示例代码片段: ```java import android.widget.ArrayAdapter; import android.widget.ListView; public class MainActivity extends AppCompatActivity { private ListView listView; private ArrayAdapter<String> adapter; private String[] data = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = findViewById(R.id.listView); // 创建适配器并设置给 ListView adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, data); listView.setAdapter(adapter); } } ``` 请注意,这只是一个示例代码片段,您可以根据自己的需求进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AaVictory.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值