先是布局界面
<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()
}
}