当前位置: 首页 > news >正文

android TAB切换

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

类似这样的布局

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

1、Xml的简单代码

      <RelativeLayoutandroid:layout_width="match_parent"android:layout_height="49dp"android:background="@drawable/bg_deal_channel_onchain"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_centerVertical="true"android:orientation="horizontal"><Viewandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1" /><Viewandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1" /></LinearLayout><LinearLayoutandroid: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" /><LinearLayoutandroid: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" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_centerVertical="true"android:orientation="horizontal"><LinearLayoutandroid: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"><TextViewandroid: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><LinearLayoutandroid: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"><TextViewandroid: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 version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"><solid android:color="@color/white" /><cornersandroid:topRightRadius="20dp"android:topLeftRadius="20dp" /><strokeandroid:width="1dp"android:color="@color/white" />
</shape>

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() {@Overridepublic void onAnimationStart(Animation animation) {}@Overridepublic 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);}@Overridepublic void onAnimationRepeat(Animation animation) {}});bgChannelSelected.startAnimation(fadeOut);AnimatorUtil.animateButtonPress(llChainMode);AnimatorUtil.animateTextTransition(this,tvChannelMode, tvChainMode, false);currentMode = 2;}

点击第一个按钮

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() {@Overridepublic void onAnimationStart(Animation animation) {}@Overridepublic 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);}@Overridepublic void onAnimationRepeat(Animation animation) {}});bgChainSelected.startAnimation(fadeOut);AnimatorUtil.animateButtonPress(llChainMode);AnimatorUtil.animateTextTransition(this,tvChannelMode, tvChainMode, true);currentMode = 1;}

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

                                                                                      -END

http://www.dtcms.com/a/564888.html

相关文章:

  • 免费试用网站源码上海网站建设穹拓
  • Linux的df和du
  • 【保姆级教程】Debian 服务器 MariaDB/Mysql 配置 Windows 远程连接全流程
  • JAVA算法练习题day58
  • linux-用户和组权限
  • 基于Vue+Python+Orange Pi Zero3的完整视频监控方案
  • 若依开源项目做导入数据时同步新增字典,页面下拉框与表格未同步更新问题
  • 网站权重多少4赤峰网站建设哪个服务好
  • 珠海seo海网站建设南京做网站建设搭建的公司
  • 仓储物流人力如何管理?实时看板动态展示进度,支持管理者即时调整人力
  • 系统架构设计师备考第62天——嵌入式系统软件架构设计方法
  • LeetCode 刷题【143. 重排链表】
  • 网站建设与管理工资wordpress仪表盘添加内容
  • 常见的分布式系统面试题清单
  • 基于 U-Net 的医学图像分割
  • 【图像处理基石】多频谱图像融合算法入门
  • 室温反应蒸发+200℃退火调控 MoOₓ/NiOₓ薄膜:光伏空穴传输材料性能优化与效率潜力(>25%)分析
  • 微算法科技(NASDAQ MLGO):DPoS驱动区块链治理与DAO机制融合,共筑Web3.0坚实基石
  • 视频直播点播平台EasyDSS:助力现代农业驶入数字科技“快车道”
  • 迈网科技 官方网站网站建设调研问卷
  • vue 实现自定义message 全局提示
  • 电商网站里的图片网站开发中 视频播放卡
  • [手机AI开发sdk] 模型冻结解冻.pb | `aidlite`加速AI模型
  • 2025 年热门 CV 会议论文【源码复现】:Neural Inverse Rendering from Propagating Light
  • 中小企业网站建设与管理南通网站排名团队
  • TypeScript 队列实战:从零实现简单、循环、双端、优先队列,附完整测试代码
  • LeetCode hot100:189 轮转数组:三种解法从入门到精通
  • 初识MYSQL —— 基本查询
  • 项目打包与部署 —— 把 Java 项目 “装瓶带走”(本地运行→服务器落地全流程)
  • 湘潭做网站价格找磐石网络一流河北网站建设哪家好