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

相关推荐
00后程序员张7 小时前
iOS 文件管理与导出实战,多工具协同打造高效数据访问与调试体系
android·macos·ios·小程序·uni-app·cocoa·iphone
Boop_wu8 小时前
[MySQL] JDBC
android
qq_717410019 小时前
FAQ09075:6572平台相机拍照,拍下来的照片无法查看,图库查看时提示“无缩略图”
android
Jerry18 小时前
Compose 的阶段
android
Zhangzy@18 小时前
Rust 编译优化选项
android·开发语言·rust
某空m20 小时前
【Android】View滑动的实现
android
芝麻开门-新起点21 小时前
Android 和 iOS 系统版本及开发适配
android·ios·cocoa
2501_9159184121 小时前
iOS描述文件功能解析
android·macos·ios·小程序·uni-app·cocoa·iphone
用户693717500138421 小时前
一文彻底搞懂 Android 依赖配置:implementation vs testImplementation,再也不混淆!
android