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

先是布局界面

复制代码
<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()
    }
}
相关推荐
BduL OWED1 天前
mysql的主从配置
android·mysql·adb
人需要PID1 天前
【卡尔曼工具箱-EKF-MATLAB应用】
android
亘元有量-流量变现1 天前
APP自动识别跳转各大应用商店(鸿蒙+iOS+安卓全品牌)|可直接部署落地页源码
android·ios·harmonyos
ForteScarlet2 天前
从 Kotlin 编译器 API 的变化开始: 2.3.20
android·开发语言·后端·ios·开源·kotlin
私人珍藏库2 天前
[Android] 假装来电 v1.1.0
android·app·工具·软件·多功能
spencer_tseng2 天前
Android Studio [Gradle JDK]
android·ide·android studio
sun0077002 天前
wifi热点的防火墙
android
Code-Porter2 天前
记录关于Flutter ObjectBox数据库使用ObjectBoxBrowser插件,在同一个局域网内电脑无法访问查看数据问题
android·数据库·flutter·dart
KongHen022 天前
Android Studio最新版汉化教程(2025年10月17日)
android·ide·android studio