本文实例为大家分享了Android实现底部切换标签的具体代码,供大家参考,具体内容如下

实现底部通用切换标签 ,嵌套Fragment,方便自定义布局

自定义控件:

widget_tab_view.xml

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/tab_image"

android:layout_width="20dp"

android:layout_height="20dp" />

android:id="@+id/tab_label"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textColor="#666666"

android:textSize="12sp" />

定义单个标签

public class TabView extends LinearLayout {

private ImageView mTabImage;

private TextView mTabLable;

public TabView(Context context) {

super(context);

initView(context);

}

public TabView(Context context, @Nullable AttributeSet attrs) {

super(context, attrs);

initView(context);

}

public TabView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

initView(context);

}

private void initView(Context context) {

setOrientation(VERTICAL);

setGravity(Gravity.CENTER);

LayoutInflater.from(context).inflate(R.layout.widget_tab_view, this, true);

mTabImage = (ImageView) findViewById(R.id.tab_image);

mTabLable = (TextView) findViewById(R.id.tab_label);

}

public void initData(TabItem tabItem) {

mTabImage.setImageResource(tabItem.imageResId);

mTabLable.setText(tabItem.lableResId);

}

}

定义单个标签的entity

public class TabItem {

public int imageResId;

public int lableResId;

public Class extends Fragment> tagFragmentClz;

public TabItem(int imageResId, int lableResId) {

this.imageResId = imageResId;

this.lableResId = lableResId;

}

public TabItem(int imageResId, int lableResId, Class extends Fragment> tagFragmentClz) {

this.imageResId = imageResId;

this.lableResId = lableResId;

this.tagFragmentClz = tagFragmentClz;

}

}

定义底部切换标签控件

public class BottomTabLayout extends LinearLayout implements View.OnClickListener {

private ArrayList tabs;

private OnTabClickListener listener;

private int tabCount;

private View selectedView;

public BottomTabLayout(Context context) {

super(context);

initView();

}

public BottomTabLayout(Context context, @Nullable AttributeSet attrs) {

super(context, attrs);

initView();

}

public BottomTabLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

initView();

}

private void initView() {

setOrientation(HORIZONTAL);

}

public void setCurrentTab(int i) {

if (i < tabCount && i >= 0) {

View view = getChildAt(i);

onClick(view);

}

}

public void initData(ArrayList tabs, OnTabClickListener listener) {

this.tabs = tabs;

this.listener = listener;

LayoutParams params = new LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT);

params.weight = 1;

params.gravity = Gravity.CENTER;

if (tabs != null && tabs.size() > 0) {

tabCount = tabs.size();

TabView mTabView = null;

for (int i = 0, len = tabs.size(); i < len; i++) {

mTabView = new TabView(getContext());

mTabView.setTag(tabs.get(i));

mTabView.initData(tabs.get(i));

mTabView.setOnClickListener(this);

addView(mTabView, params);

}

} else {

throw new IllegalArgumentException("tabs can not be empty");

}

}

@Override

public void onClick(View view) {

if (selectedView != view) {

listener.onTabClick((TabItem) view.getTag());

view.setSelected(true);

if (selectedView != null) {

selectedView.setSelected(false);

}

selectedView = view;

}

}

public interface OnTabClickListener {

void onTabClick(TabItem tabItem);

}

}

Activity

public class MainActivity extends AppCompatActivity implements BottomTabLayout.OnTabClickListener {

private BottomTabLayout tab_layout;

private ArrayList tabs;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

setTitle("底部切换标签");

tab_layout = (BottomTabLayout) findViewById(R.id.tab_layout);

initBottomTab();

tab_layout.setCurrentTab(0);

}

private void initBottomTab() {

tabs = new ArrayList<>();

tabs.add(new TabItem(R.drawable.selector_tab_msg, R.string.wechat, OneFragment.class));

tabs.add(new TabItem(R.drawable.selector_tab_contact, R.string.contacts, TwoFragment.class));

tabs.add(new TabItem(R.drawable.selector_tab_moments, R.string.discover, ThreeFragment.class));

tabs.add(new TabItem(R.drawable.selector_tab_profile, R.string.me, FourFragment.class));

tab_layout.initData(tabs, this);

}

private Fragment lastFragment;

@Override

public void onTabClick(TabItem tabItem) {

try {

Fragment tmpFragment = getSupportFragmentManager().findFragmentByTag(tabItem.tagFragmentClz.getSimpleName());

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

if (tmpFragment == null) {

tmpFragment = tabItem.tagFragmentClz.newInstance();

transaction.add(R.id.fl_container, tmpFragment, tabItem.tagFragmentClz.getSimpleName());

if (lastFragment != null) {

transaction.hide(lastFragment);

}

transaction.commitAllowingStateLoss();

} else {

transaction.show(tmpFragment);

if (lastFragment != null) {

transaction.hide(lastFragment);

}

transaction.commitAllowingStateLoss();

}

lastFragment = tmpFragment;

} catch (Exception e) {

e.printStackTrace();

}

}

}

布局文件

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context="com.sample.bottomtab.MainActivity">

android:id="@+id/fl_container"

android:layout_width="match_parent"

android:layout_height="0dp"

android:layout_weight="1"

android:background="#ffffff" />

android:layout_width="match_parent"

android:layout_height="1dp"

android:background="#dcdcdc" />

android:id="@+id/tab_layout"

android:layout_width="match_parent"

android:layout_height="48dp"

android:background="#ffffff" />

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

android 顶部标签切换,Android实现底部切换标签相关推荐

  1. Android 顶部滑动切换实现(一)

    先看效果图,在导航栏下面分类的两个切换按钮,页面滑动也能像微信底部导航一样实现跳转. 下面看主要代码. 布局文件. <RelativeLayout android:id="@+id/c ...

  2. android顶部导航高度,Android特效——————底部/顶部导航条(Fragment+ViewPaper+XTabLayout)...

    初次使用xtablayout和viewpaper2.所以就弄了最基础的导航条 一.效果 二.代码 配置环境[在bulid.gradle中添加以下代码] implementation 'androidx ...

  3. android+tv+自动切换,Android TV 重写GridView,实现焦点放大效果

    关于缩放,使用了view.setScaleX/Y 方法,api11以上即可. 重写dispatchDraw(),绘制选中项的焦点效果.(注意带阴影的焦点图需要微调偏移量) 要将选中项绘制显示在顶层,所 ...

  4. Android fragmnet标签,在Android中为Fragment添加标签?

    我试图在Fragment中添加一个TabHost.代码如下.这里里面的Fragment.我试图添加TabHost显示两个标签: package com.nordicsoft.dilosysNewVer ...

  5. android 相册 标签,在Android标签上,如何在图片下方显示文字?

    我有一个使用标签的Android应用程序. 我已经有选项卡,并且在应用程序上正确存在"活动". 我的问题是,当我在选项卡上添加文本和图像时,文本显示在图像上.(EXAMPLE) 我 ...

  6. android 导航自动切换,Android导航抽屉切换图标向右

    吃鸡游戏 我为EndDrawerToggle该类编写了一个与您的设置非常相似的设置- DrawerLayout带末端对齐的抽屉View,AppCompatActivity带有自定义Toolbar的支持 ...

  7. android 横竖屏幕切换,Android 横竖屏切换总结

    一.Android切换横竖屏 应用的横竖屏设置 应用的横竖屏设置主要是通过Activity的screenOrientation属性控制,属性值如下: 主要有以下两种方式设置screenOrientat ...

  8. android nfc标签类型,Android NFC标签 开发深度解析 触碰的艺术

    原标题:Android NFC标签 开发深度解析 触碰的艺术 本文来自于CSDN博客,作者:郭朝,已获授权,版权归原作者所有,未经作者同意,请勿转载. 欢迎同有博客好文章的作者加微信(ID:tm_fo ...

  9. android 表情键盘切换,Android仿微信键盘切换效果

    Android 仿微信的键盘切换(录音,表情,文字,其他),IM通讯,类似朋友圈只要涉及到文字等相关的app都会要涉及到键盘的处理,今天就给大家分享一下Android 仿微信的键盘切换. 效果图如下: ...

最新文章

  1. linux电脑培训,电脑培训Linux服务器初始化Shell
  2. cuSPARSE库:(八)cusparseGetStream()
  3. string 方法 java_String 的几个 方法。 (java)
  4. java求sobel算子代码_sobel算子原理与实现
  5. jstack命令报错
  6. 华硕笔记本linux触摸板驱动,华硕笔记本触摸板驱动安装教程及打开方法
  7. sqlite转sql2000数据库
  8. snipaste如何滚动截图_试用了20个截图工具,我写下这份超全的软件指南。?
  9. 国外计算机论文范文精选,国外计算机论文参考范文.doc
  10. 0202插入删除-算法第四版红黑树-红黑树-数据结构和算法(Java)
  11. 加班申请 ----中间表--系统自动算出---可调休天数
  12. 三菱PLC控制步进驱动器脉冲定位相关(附代码接线图)
  13. 用半年的时间面试自己
  14. php 时间加法函数_PHP 日期时间函数的高级应用技巧
  15. Centos 进入recovery模式,单用户模式
  16. xcode打包报错Command CodeSign failed with a nonzero exit code的解决方案
  17. python发邮件给女朋友代码_用python讨好女朋友是什么个操作?
  18. python中的subprocess.Popen()使用详解---以及注意的问题(死锁)
  19. ICC II 4 timing setup(MCMM的设置)
  20. Python 模拟登陆神库!集合了20+个平台的模拟登陆脚本

热门文章

  1. 高效程序员的 7 项技能
  2. python中的集合set
  3. java从1开始计时用线程_java – Python – 线程,计时或函数使用?
  4. mysql注入单引号被过滤_证明过滤单引号的ORDER BY可以注入
  5. 推荐系统常用评价指标和代码实现
  6. ICML2021 | Self-Tuning: 如何减少对标记数据的需求?
  7. CentOS6上Hadoop集群中服务器cpu sys态异常的定位与解决
  8. Spring Cloud Stream如何处理消息重复消费
  9. 中文实体命名识别工具使用汇总:Stanza、LAC、Ltp、Hanlp、foolnltk、NLTK、BosonNLP
  10. 实时事理学习与搜索平台DemoV1.0正式对外发布