自定义 按钮间,按钮边框滑动。

先是布局界面

复制代码
<RelativeLayout
                android:id="@+id/tab_button_rel"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/toolbar"
                >

                <androidx.appcompat.widget.AppCompatButton
                    android:id="@+id/btnOrderList"
                    android:text="订单列表"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="2dp"
                    android:background="@drawable/tab_button_background"
                    />

                <androidx.appcompat.widget.AppCompatButton
                    android:id="@+id/btnChargingOrderList"
                    android:text="充电宝列表"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:layout_marginTop="2dp"
                    android:background="@drawable/tab_button_background"
                    />

                <androidx.appcompat.widget.AppCompatButton
                    android:id="@+id/btnExceptionOrderList"
                    android:text="异常列表"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:layout_alignParentRight="true"
                    android:layout_marginEnd="10dp"
                    android:layout_marginTop="2dp"
                    android:background="@drawable/tab_button_background"
                    />

                <View
                    android:id="@+id/border"
                    android:layout_width="1dp"
                    android:layout_height="50dp"
                    android:background="@drawable/border_button_background" />
            </RelativeLayout>

然后是几个样式文件。

复制代码
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true">
        <shape android:shape="rectangle">
            <solid android:color="@android:color/white" />
            <stroke
                android:width="1dp"
                android:color="@android:color/holo_red_dark" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@android:color/white" />
            <stroke
                android:width="1dp"
                android:color="@android:color/holo_blue_dark" />
        </shape>
    </item>
</selector>

另一个border_button_background

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

    <!-- Transparent middle part -->
    <item android:drawable="@android:color/transparent" />

    <!-- Colored border -->
    <item
        android:left="1dp"
        android:right="1dp"
        android:top="1dp"
        android:bottom="1dp">
        <shape android:shape="rectangle">
            <solid android:color="@android:color/holo_blue_dark" />
        </shape>
    </item>

</layer-list>

使用代码

Kotlin 复制代码
class YourFragment : Fragment() {

    private var selectedButton: Button? = null

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_your_layout, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val buttons = listOf(btnOrderList, btnChargingOrderList, btnExceptionOrderList)
        val border = view.findViewById<View>(R.id.border)

        buttons.forEachIndexed { index, button ->
            button.setOnClickListener {
                selectButton(button)
                animateBorder(button, border)
            }
        }

        // Set initial width of the border to match the first button
        val firstButton = buttons.first()
        border.layoutParams.width = firstButton.width
        border.requestLayout()
    }

    private fun selectButton(button: Button) {
        selectedButton?.isSelected = false
        button.isSelected = true
        selectedButton = button
    }

    private fun animateBorder(selectedButton: Button, border: View) {
        val targetX = selectedButton.x
        val targetWidth = selectedButton.width

        border.animate()
            .x(targetX)
            .scaleX(targetWidth / border.width.toFloat())
            .setDuration(300)
            .start()
    }
}
相关推荐
。puppy20 小时前
MySQL 远程登录实验:通过 IP 地址跨机器连接实战指南
android·adb
dongdeaiziji20 小时前
深入理解 Kotlin 中的构造方法
android·kotlin
风起云涌~20 小时前
【Android】浅谈Navigation
android
游戏开发爱好者821 小时前
iOS 商店上架全流程解析 从工程准备到审核通过的系统化实践指南
android·macos·ios·小程序·uni-app·cocoa·iphone
QuantumLeap丶1 天前
《Flutter全栈开发实战指南:从零到高级》- 18 -自定义绘制与画布
android·flutter·ios
.豆鲨包1 天前
【Android】 View事件分发机制源码分析
android·java
花落归零1 天前
Android 小组件AppWidgetProvider的使用
android
弥巷1 天前
【Android】常见滑动冲突场景及解决方案
android·java
angushine1 天前
解决MySQL慢日志输出问题
android·数据库·mysql
fouryears_234171 天前
Android 与 Flutter 通信最佳实践 - 以分享功能为例
android·flutter·客户端·dart