Android 自定义样式实现好看的SwitchCompat效果

SwitchCompat自定义样式笔记

UI设计图加粗样式

按钮效果图

  • 原生的控件一般是thumb(滑块)比滑道要稍微大一点,这里只能是通过图片或者自己画drawable实现
    如标题所言,这里肯定是通过代码实现了

xml代码

<androidx.appcompat.widget.SwitchCompat
        android:id="@+id/switch_check"
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="20dp"
        android:thumb="@drawable/switch_common_thumb"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:switchMinWidth="60dp"
        app:track="@drawable/switch_common_track" />

thumb代码

  • 这里使用stroke属性将边缘设置成透明就可以实现滑块比滑道小的效果
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="@color/dialog_sync" />
    <size
        android:width="30dp"
        android:height="30dp" />
    <stroke
        android:width="10dp"
        android:color="#00000000" />
    <corners android:radius="15dp" />
</shape>

滑块代码和效果图

track代码

  • 滑道看效果图的话,一个drawable实现不了,因此这里通过layer-list叠加画出,底层是一个中间透明,边缘蓝色的矩形,上层是一个外部透明,中间淡蓝色的矩形,这样子就叠加出滑块的效果图了
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#00000000" />
            <size android:height="30dp" />
            <stroke
                android:width="3dp"
                android:color="@color/dialog_sync" />
            <corners android:radius="15dp" />
        </shape>
    </item>

    <item>
        <shape android:shape="rectangle">
            <solid android:color="#255A9FFF" />
            <size android:height="20dp" />
            <stroke
                android:width="12dp"
                android:color="#00000000" />
            <corners android:radius="15dp" />
        </shape>
    </item>
</layer-list>

关闭时的滑块效果

  • 滑道因为有开关两种状态,上面已经实现了关闭时的效果,因此还需要实现一个打开时的效果
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#00000000" />
            <size android:height="30dp" />
            <stroke
                android:width="3dp"
                android:color="@color/dialog_sync" />
            <corners android:radius="15dp" />
        </shape>
    </item>

    <item >
        <shape android:shape="rectangle">
            <solid android:color="#A85A9FFF" />
            <size android:height="20dp" />
            <stroke
                android:width="12dp"
                android:color="#00000000" />
            <corners android:radius="15dp" />
        </shape>
    </item>
</layer-list>

打开时的滑块效果

  • 最后通过选择器将他们组合在一起,就实现了最上面的效果了
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/switch_common_track_selected" android:state_checked="true" />
    <item android:drawable="@drawable/switch_common_track_unselected" android:state_checked="false" />
</selector>

switch_off switch_on

  • 7
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
首先需要了解一下 Android 中通知的相关知识。Android 中的通知是通过 NotificationManager 来管理的,通知的显示效果是由 Notification 类的实例来控制的。一般情况下,我们可以使用 NotificationCompat 类来构造通知,可以兼容不同版本的 Android 系统。 接下来,我们来介绍一下如何通过自定义 Toast 实现悬浮通知效果: 1. 首先,在 AndroidManifest.xml 文件中添加权限声明: ```xml <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/> ``` 2. 在代码中创建自定义的 Toast 类,并重写其 onWindowFocusChanged() 方法,用于创建悬浮通知: ```java public class FloatingToast extends Toast { private WindowManager mWindowManager; private View mView; private WindowManager.LayoutParams mParams; public FloatingToast(Context context) { super(context); mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); mView = LayoutInflater.from(context).inflate(R.layout.floating_toast, null); mParams = new WindowManager.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT); mParams.gravity = Gravity.TOP | Gravity.START; } @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); if (hasFocus) { mWindowManager.addView(mView, mParams); } else { mWindowManager.removeView(mView); } } } ``` 3. 在布局文件 floating_toast.xml 中定义悬浮通知的样式: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/floating_toast" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/bg_floating_toast" android:orientation="horizontal"> <ImageView android:id="@+id/iv_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_notification" /> <TextView android:id="@+id/tv_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这是一条悬浮通知" /> </LinearLayout> ``` 4. 在 Activity 中使用自定义 Toast 实现悬浮通知: ```java FloatingToast toast = new FloatingToast(this); toast.setDuration(Toast.LENGTH_LONG); toast.setView(LayoutInflater.from(this).inflate(R.layout.floating_toast, null)); toast.show(); ``` 5. 最后,记得在 Activity 的 onDestroy() 方法中销毁自定义 Toast 对象: ```java @Override protected void onDestroy() { super.onDestroy(); if (toast != null) { toast.cancel(); } } ``` 上述代码中的布局文件和相关资源文件可以根据需要自行修改,以实现不同的悬浮通知样式。同时,需要注意的是,由于 Android 8.0 及以上版本对通知权限进行了限制,如果需要在这些系统版本上显示悬浮通知,需要申请权限并设置 targetSdkVersion 为 25 或以下。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值