美文网首页
Android开发之用xml写出按钮

Android开发之用xml写出按钮

作者: 南风知我咦 | 来源:发表于2023-04-12 22:30 被阅读0次

开发问题

  • 啊,开发的时候,页面有一个开关一样的按钮啊,然后UI只给我切了OPEN状态的图啊,没办法只能想办法实现了。

牛逼

  • 尝试了一下用xml实现了。

xml文件

  • bg_switch_button_close
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:width="50dp"
        android:height="30dp">
        <shape android:shape="rectangle">
            <solid android:color="@color/color_EEEEEE" />
            <corners
                android:bottomLeftRadius="30dp"
                android:bottomRightRadius="30dp"
                android:topLeftRadius="30dp"
                android:topRightRadius="30dp" />
        </shape>
    </item>
    <item
        android:width="28dp"
        android:height="28dp"
        android:gravity="center_vertical"
        android:left="2dp">
        <shape android:shape="oval">
            <solid android:color="@color/white" />
        </shape>
    </item>
</layer-list>
  • 效果


    close.png
  • bg_switch_button_open

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:width="50dp"
        android:height="30dp">
        <shape android:shape="rectangle">
            <solid android:color="@color/white" />
            <corners
                android:bottomLeftRadius="30dp"
                android:bottomRightRadius="30dp"
                android:topLeftRadius="30dp"
                android:topRightRadius="30dp" />
        </shape>
    </item>
    <item
        android:width="28dp"
        android:height="28dp"
        android:gravity="center_vertical|right"
        android:right="2dp">
        <shape android:shape="oval">
            <solid android:color="@color/color_007aff" />
        </shape>
    </item>
</layer-list>
  • 效果


    open.png
  • 使用

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btn_open"
        android:layout_width="50dp"
        android:layout_height="30dp"
        android:layout_marginLeft="13dp"
        android:layout_marginTop="13dp"
        android:background="@drawable/bg_switch_button_open"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <Button
        android:id="@+id/btn_close"
        android:layout_width="50dp"
        android:layout_height="30dp"
        android:layout_marginLeft="13dp"
        android:layout_marginTop="13dp"
        android:background="@drawable/bg_switch_button_close"
        app:layout_constraintLeft_toRightOf="@+id/btn_open"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
  • 整体效果


    效果.png
  • 还行吧。
  • 所以xml文件能实现好多东西啊。以后还有有趣的再写。

相关文章

网友评论

      本文标题:Android开发之用xml写出按钮

      本文链接:https://www.haomeiwen.com/subject/lbnoddtx.html