android:id=“@+id/rl_me”

android:layout_width=“0dp”

android:layout_height=“wrap_content”

android:layout_weight=“1.0” >

android:id=“@+id/iv_me”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_centerHorizontal=“true”

android:src=“@drawable/btn_my_nor”

android:contentDescription=“@null” />

android:id=“@+id/tv_me”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_below=“@+id/iv_me”

android:layout_centerHorizontal=“true”

android:text=“@string/bottom_tab_my”

android:textColor=“@color/bottomtab_normal”

android:textSize=“12sp” />

android:id=“@+id/content_layout”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:layout_above=“@+id/line”

android:layout_below=“@+id/top_tab”

android:orientation=“vertical” >

android:id=“@+id/line”

android:layout_width=“match_parent”

android:layout_height=“1dp”

android:layout_above=“@id/ll_bottom_tab”

android:background=“@color/line” />

以上是布局代码,下面就介绍如何通过点击标签切换Fragment:

我们会发现,初始的时候是选中第一个标签页,图片和字体的颜色区别于另外两个标签页,所以我们要做的就是切换标签的时候,就改变标签的状态

主要改两个内容:

图片 文字颜色

然后我们切换标签显示的是不同的Fragment,这里我们有三个Fragment,所以我们定义三个不同的Fragment界面:

/BottomTabDemo/src/com/xiaowu/bottomtab/demo/ZhidaoFragment.java

/BottomTabDemo/src/com/xiaowu/bottomtab/demo/IWantKnowFragment.java

/BottomTabDemo/src/com/xiaowu/bottomtab/demo/MeFragment.java

每个Fragment对应不同的布局文件:

/BottomTabDemo/res/layout/main_tab1_fragment.xml

/BottomTabDemo/res/layout/main_tab2_fragment.xml

/BottomTabDemo/res/layout/main_tab3_fragment.xml

ok,这些定义好之后,我们就在主界面上编写切换的代码了,如何对Fragment进行切换呢,定义以下方法:

/**

添加或者显示碎片 @param transaction @param fragment

*/

private void addOrShowFragment(FragmentTransaction transaction,

Fragment fragment) {

if (currentFragment == fragment)

return;

if (!fragment.isAdded()) { // 如果当前fragment未被添加,则添加到Fragment管理器中

transaction.hide(currentFragment)

.add(R.id.content_layout, fragment).commit();

} else {

transaction.hide(currentFragment).show(fragment).commit();

}

currentFragment = fragment;

}

完整代码如下:

/BottomTabDemo/src/com/xiaowu/bottomtab/demo/MainActivity.java

package com.xiaowu.bottomtab.demo;

import android.os.Bundle;

import android.support.v4.app.Fragment;

import android.support.v4.app.FragmentActivity;

import android.support.v4.app.FragmentTransaction;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.ImageView;

import android.widget.RelativeLayout;

import android.widget.TextView;

/**

主Activity @author wwj_748

*/

public class MainActivity extends FragmentActivity implements OnClickListener {

// 三个tab布局

private RelativeLayout knowLayout, iWantKnowLayout, meLayout;

// 底部标签切换的Fragment

private Fragment knowFragment, iWantKnowFragment, meFragment,

currentFragment;

// 底部标签图片

private ImageView knowImg, iWantKnowImg, meImg;

// 底部标签的文本

private TextView knowTv, iWantKnowTv, meTv;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initUI();

initTab();

}

/**

初始化UI

*/

private void initUI() {

knowLayout = (RelativeLayout) findViewById(R.id.rl_know);

iWantKnowLayout = (RelativeLayout) findViewById(R.id.rl_want_know);

meLayout = (RelativeLayout) findViewById(R.id.rl_me);

knowLayout.setOnClickListener(this);

iWantKnowLayout.setOnClickListener(this);

meLayout.setOnClickListener(this);

knowImg = (ImageView) findViewById(R.id.iv_know);

iWantKnowImg = (ImageView) findViewById(R.id.iv_i_want_know);

meImg = (ImageView) findViewById(R.id.iv_me);

knowTv = (TextView) findViewById(R.id.tv_know);

iWantKnowTv = (TextView) findViewById(R.id.tv_i_want_know);

meTv = (TextView) findViewById(R.id.tv_me);

}

/**

初始化底部标签

*/

private void initTab() {

if (knowFragment == null) {

knowFragment = new ZhidaoFragment();

}

if (!knowFragment.isAdded()) {

// 提交事务

getSupportFragmentManager().beginTransaction()

.add(R.id.content_layout, knowFragment).commit();

// 记录当前Fragment

currentFragment = knowFragment;

// 设置图片文本的变化

knowImg.setImageResource(R.drawable.btn_know_pre);

knowTv.setTextColor(getResources()

.getColor(R.color.bottomtab_press));

iWantKnowImg.setImageResource(R.drawable.btn_wantknow_nor);

iWantKnowTv.setTextColor(getResources().getColor(

R.color.bottomtab_normal));

meImg.setImageResource(R.drawable.btn_my_nor);

meTv.setTextColor(getResources().getColor(R.color.bottomtab_normal));

}

}

@Override

public void onClick(View view) {

switch (view.getId()) {

case R.id.rl_know: // 知道

clickTab1Layout();

break;

case R.id.rl_want_know: // 我想知道

clickTab2Layout();

break;

case R.id.rl_me: // 我的

clickTab3Layout();

break;

default:

break;

}

}

/**

点击第一个tab

*/

private void clickTab1Layout() {

if (knowFragment == null) {

knowFragment = new ZhidaoFragment();

}

addOrShowFragment(getSupportFragmentManager().beginTransaction(), knowFragment);

// 设置底部tab变化

knowImg.setImageResource(R.drawable.btn_know_pre);

knowTv.setTextColor(getResources().getColor(R.color.bottomtab_press));

iWantKnowImg.setImageResource(R.drawable.btn_wantknow_nor);

iWantKnowTv.setTextColor(getResources().getColor(

R.color.bottomtab_normal));

meImg.setImageResource(R.drawable.btn_my_nor);

meTv.setTextColor(getResources().getColor(R.color.bottomtab_normal));

}

/**

点击第二个tab

*/

private void clickTab2Layout() {

if (iWantKnowFragment == null) {

iWantKnowFragment = new IWantKnowFragment();

}

addOrShowFragment(getSupportFragmentManager().beginTransaction(), iWantKnowFragment);

knowImg.setImageResource(R.drawable.btn_know_nor);

knowTv.setTextColor(getResources().getColor(R.color.bottomtab_normal));

iWantKnowImg.setImageResource(R.drawable.btn_wantknow_pre);

iWantKnowTv.setTextColor(getResources().getColor(

R.color.bottomtab_press));

meImg.setImageResource(R.drawable.btn_my_nor);

meTv.setTextColor(getResources().getColor(R.color.bottomtab_normal));

}

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

结尾

我还总结出了互联网公司Android程序员面试涉及到的绝大部分面试题及答案,并整理做成了文档,以及系统的进阶学习视频资料分享给大家。 (包括Java在Android开发中应用、APP框架知识体系、高级UI、全方位性能调优,NDK开发,音视频技术,人工智能技术,跨平台技术等技术资料),希望能帮助到你面试前的复习,且找到一个好的工作,也节省大家在网上搜索资料的时间来学习。

《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门即可获取!

体系化!**

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

结尾

我还总结出了互联网公司Android程序员面试涉及到的绝大部分面试题及答案,并整理做成了文档,以及系统的进阶学习视频资料分享给大家。 (包括Java在Android开发中应用、APP框架知识体系、高级UI、全方位性能调优,NDK开发,音视频技术,人工智能技术,跨平台技术等技术资料),希望能帮助到你面试前的复习,且找到一个好的工作,也节省大家在网上搜索资料的时间来学习。

[外链图片转存中…(img-iqdGHjjj-1711762914745)]

《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门即可获取!

精彩文章

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: