android TAB切换

有一些场景需要左右两个按钮tab实现切换的布局,如图:

类似这样的布局

这种只是在xml中实现的简单布局,没有切换时的动画效果,

1、Xml的简单代码

XML 复制代码
      <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="49dp"
                android:background="@drawable/bg_deal_channel_onchain">


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_centerVertical="true"

                    android:orientation="horizontal">

                    <View
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1" />

                    <View
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1" />
                </LinearLayout>


                <LinearLayout
                    android:id="@+id/bg_channel_selected"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_centerVertical="true"
                    android:background="@mipmap/bg_left"
                    android:orientation="horizontal"
                    android:visibility="visible" />


                <LinearLayout
                    android:id="@+id/bg_chain_selected"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_centerVertical="true"
                    android:background="@mipmap/bg_right"
                    android:orientation="horizontal"
                    android:visibility="gone" />


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_centerVertical="true"

                    android:orientation="horizontal">


                    <LinearLayout
                        android:id="@+id/ll_channel_mode"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:background="@android:color/transparent"
                        android:gravity="center"
                        android:onClick="onModeSwitchClick">

                        <TextView
                            android:id="@+id/tv_channel_mode"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="通道"
                            android:textColor="@color/white"
                            android:textSize="15sp"
                            android:textStyle="bold" />
                    </LinearLayout>


                    <LinearLayout
                        android:id="@+id/ll_chain_mode"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:background="@android:color/transparent"
                        android:gravity="center"
                        android:onClick="onModeSwitchClick">

                        <TextView
                            android:id="@+id/tv_chain_mode"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="链上"
                            android:textColor="@color/black"
                            android:textSize="15sp"
                            android:textStyle="bold" />
                    </LinearLayout>

                </LinearLayout>

            </RelativeLayout>

资源文件bg_deal_channel_onchain

XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/white" />
    <corners
        android:topRightRadius="20dp"
        android:topLeftRadius="20dp" />
    <stroke
        android:width="1dp"
        android:color="@color/white" />
</shape>

java代码

点击第二个按钮

java 复制代码
public void switchToChainMode() {
        tab_onchain.setVisibility(View.VISIBLE);

        if (currentMode == 2) return;

        AlphaAnimation fadeOut = new AlphaAnimation(1.0f, 0.0f);
        fadeOut.setDuration(150);
        fadeOut.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {}

            @Override
            public void onAnimationEnd(Animation animation) {
                bgChannelSelected.setVisibility(View.GONE);
                bgChainSelected.setVisibility(View.VISIBLE);
                AlphaAnimation fadeIn = new AlphaAnimation(0.0f, 1.0f);
                fadeIn.setDuration(150);
                bgChainSelected.startAnimation(fadeIn);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {}
        });
        bgChannelSelected.startAnimation(fadeOut);
        AnimatorUtil.animateButtonPress(llChainMode);
        AnimatorUtil.animateTextTransition(this,tvChannelMode, tvChainMode, false);

        currentMode = 2;
    }

点击第一个按钮

java 复制代码
public void switchToChannelMode() {
     
        tab_onchain.setVisibility(View.GONE);
        if (currentMode == 1) return;

        AlphaAnimation fadeOut = new AlphaAnimation(1.0f, 0.0f);
        fadeOut.setDuration(150);
        fadeOut.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {}

            @Override
            public void onAnimationEnd(Animation animation) {
                bgChainSelected.setVisibility(View.GONE);
                bgChannelSelected.setVisibility(View.VISIBLE);
                AlphaAnimation fadeIn = new AlphaAnimation(0.0f, 1.0f);
                fadeIn.setDuration(150);
                bgChannelSelected.startAnimation(fadeIn);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {}
        });
        bgChainSelected.startAnimation(fadeOut);
        AnimatorUtil.animateButtonPress(llChainMode);
        AnimatorUtil.animateTextTransition(this,tvChannelMode, tvChainMode, true);

        currentMode = 1;
    }

2、其中动画可根据具体需求实现。

-END

相关推荐
我命由我123456 分钟前
Android 广播 - 静态注册与动态注册对广播接收器实例创建的影响
android·java·开发语言·java-ee·android studio·android-studio·android runtime
朗迹 - 张伟42 分钟前
Tauri2 导出 Android 详细教程
android
lpruoyu1 小时前
【Android第一行代码学习笔记】Android架构_四大组件_权限_持久化_通知_异步_服务
android·笔记·学习
独自破碎E2 小时前
【BISHI15】小红的夹吃棋
android·java·开发语言
李堇5 小时前
android滚动列表VerticalRollingTextView
android·java
lxysbly7 小时前
n64模拟器安卓版带金手指2026
android
游戏开发爱好者89 小时前
日常开发与测试的 App 测试方法、查看设备状态、实时日志、应用数据
android·ios·小程序·https·uni-app·iphone·webview
王码码203510 小时前
Flutter for OpenHarmony 实战之基础组件:第三十一篇 Chip 系列组件 — 灵活的标签化交互
android·flutter·交互·harmonyos
黑码哥10 小时前
ViewHolder设计模式深度剖析:iOS开发者掌握Android列表性能优化的实战指南
android·ios·性能优化·跨平台开发·viewholder
亓才孓10 小时前
[JDBC]元数据
android