android动态添加顶部tabbar,Android自定义底部导航栏-Tabbar

本文介绍如何在Android应用中实现动态添加顶部Tabbar,并展示了一个使用ButterKnife绑定事件、切换Fragment的示例代码。通过创建自定义布局和Fragment,实现了点击Tabbar切换显示内容的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、添加依赖

//view,事件绑定

implementation 'com.jakewharton:butterknife:8.8.1'

annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

二、开始写布局

1、Tabbar布局页面

2、fragment_test.xml布局页面

这个页面就是FrameLayout 中要显示的页面

三、开始写代码

1、ShowTabbarActivity.Activity

import android.os.Bundle;

import android.support.v4.app.FragmentManager;

import android.support.v4.app.FragmentTransaction;

import android.support.v7.app.AppCompatActivity;

import android.view.View;

import android.widget.FrameLayout;

import android.widget.ImageView;

import android.widget.LinearLayout;

import android.widget.TextView;

import com.bh.pql.Fragment.TestFragment1;

import com.bh.pql.Fragment.TestFragment2;

import butterknife.BindView;

import butterknife.ButterKnife;

import butterknife.OnClick;

public class ShowtabbarActivity extends AppCompatActivity {

@BindView(R.id.content)

FrameLayout flayout;

@BindView(R.id.tabbar_group)

ImageView tabbarGroup;

@BindView(R.id.tabbar_group_text)

TextView tabbarGroupText;

@BindView(R.id.tabbar_mine)

ImageView tabbarMine;

@BindView(R.id.tabbar_mine_text)

TextView tabbarMineText;

@BindView(R.id.tabbar_group_layout)

LinearLayout tabbarGroupLayout;

@BindView(R.id.tabbar_mine_layout)

LinearLayout tabbarMineLayout;

private TestFragment1 testFragment1;

private TestFragment2 testFragment2;

FragmentManager fragmentManager;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

getSupportActionBar().hide();

setContentView(R.layout.activity_showtabbar);

ButterKnife.bind(this);

fragmentManager = getSupportFragmentManager(); // 开启一个事物

// 默认加载某一个tabbarItem(第一个按钮)

testFragment1 = new TestFragment1();

// 启动Activity时使第一个按钮的图标为选中状态(投机取巧)

tabbarGroup.setImageResource(R.drawable.tabbar_group_select);

tabbarGroupText.setTextColor(getResources().getColor(R.color.colorAccent));

getSupportFragmentManager().beginTransaction().add(R.id.content, testFragment1).commit();

}

@OnClick({R.id.tabbar_group_layout, R.id.tabbar_mine_layout})

public void onViewClicked(View view) {

clearChioce();

// 重置选项+隐藏所有的Fragment

FragmentTransaction transaction = fragmentManager.beginTransaction();

hideFragments(transaction);

switch (view.getId()) {

case R.id.tabbar_group_layout:

hideFragments(transaction);

tabbarGroup.setImageResource(R.drawable.tabbar_group_select);

tabbarGroupText.setTextColor(getResources().getColor(R.color.colorAccent));

if (testFragment1 == null) {

testFragment1 = new TestFragment1();

transaction.add(R.id.content, testFragment1);

} else {

transaction.show(testFragment1);

}

break;

case R.id.tabbar_mine_layout:

hideFragments(transaction);

tabbarMine.setImageResource(R.drawable.tabbar_mine_select);

tabbarMineText.setTextColor(getResources().getColor(R.color.colorAccent));

if (testFragment2 == null) {

testFragment2 = new TestFragment2();

transaction.add(R.id.content, testFragment2);

} else {

transaction.show(testFragment2);

}

break;

}

transaction.commit();

}

/**

* 将所有的Fragment都设置为隐藏状态

*

* @param transaction 事物

*/

private void hideFragments(FragmentTransaction transaction) {

if (testFragment1 != null) {

transaction.hide(testFragment1);

}

if (testFragment2 != null) {

transaction.hide(testFragment2);

}

}

/**

* 定义一个重置所有选项的方法

*/

public void clearChioce() {

tabbarGroup.setImageResource(R.drawable.tabbar_group_normal);

tabbarGroupText.setTextColor(getResources().getColor(R.color.colorTextGray));

tabbarMine.setImageResource(R.drawable.tabbar_mine_normal);

tabbarMineText.setTextColor(getResources().getColor(R.color.colorTextGray));

}

}

2、TestFragment1

必须是继承Fragment的类

public class TestFragment1 extends Fragment {

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.activity_pt_listview_heade, container,false);

return view;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值