Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Android Activity与Fragment实现底部导航器大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

单Activity多Fragment实现底部导航器@H_801_2@

最近由于Android基础知识讲解需要,采用单Activity多Fragment实现类似QQ底部导航器示例,这种开发模式广泛应用于App开发,比如QQ,微信,新浪等,关于Android底部导航栏的实现方式特别多,实现也是五花八门,同时Google在自己推出的Material design中也增加了Bottom Navigation导航控制,实现起来更加简单,且支持动态效果更加酷炫,但是因为是基础的知识,所以打算通过自定义来实现,不使用Bottom Navigation(如果是实际的项目开发可以虑使用哈~~),希望对初学者有所帮助。

BottomNavigationBar 地址:https://github.com/Ashok-Varma/BottomNavigation

分析@H_801_2@

整体区域分为两部分:底部的导航区域和其余的内容区域,内容区域使用Fragment最为UI展示,底部导航区域添加点击事件,点击TAB切换Fragment,并做字体颜色和图片的切换,在切换上使用add()和hide()的方式,不使用replase(),至于两者的区别,这里就不再说明了,感兴趣的话可以谷歌。

Android Activity与Fragment实现底部导航器


整体结构

底部导航通常由图片和@L_618_15@组成,上面为ImageView,下面为TextView

Android Activity与Fragment实现底部导航器


底部导航

底部Bottom

使用布局使用RelativeLayout,添加weight,平分屏幕宽度

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://scheR_907_11845@as.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="@dimen/bottom_height"
  android:BACkground="@color/base_white"
  android:orientation="vertical">

  <View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:BACkground="@color/gray_line" />

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <RelativeLayout
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="1">

      <RelativeLayout
        android:id="@+id/layout_home"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true">

        <ImageView
          android:id="@+id/img_home"
          android:layout_width="24dp"
          android:layout_height="24dp"
          android:layout_centerHorizontal="true"
          android:src="@mipmap/icon_home_pressed" />

        <TextView
          android:id="@+id/tv_home"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layouT_Below="@id/img_home"
          android:layout_centerHorizontal="true"
          android:text="首页"
          android:textColor="@color/bottom_text_color_pressed"
          android:textSize="12sp" />
      </RelativeLayout>

    </RelativeLayout>

    <RelativeLayout

      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="1">

      <RelativeLayout
        android:id="@+id/layout_health"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true">

        <ImageView
          android:id="@+id/img_health"
          android:layout_width="24dp"
          android:layout_height="24dp"
          android:layout_centerHorizontal="true"
          android:src="@mipmap/icon_health_normal" />

        <TextView
          android:id="@+id/tv_health"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layouT_Below="@id/img_health"
          android:layout_centerHorizontal="true"
          android:text="健康"
          android:textColor="@color/bottom_text_color_normal"
          android:textSize="12sp" />
      </RelativeLayout>
    </RelativeLayout>

    <RelativeLayout

      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="1">

      <RelativeLayout
        android:id="@+id/layout_msg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true">

        <ImageView
          android:id="@+id/img_msg"
          android:layout_width="24dp"
          android:layout_height="24dp"
          android:layout_centerHorizontal="true"
          android:src="@mipmap/icon_msg_normal" />

        <TextView
          android:id="@+id/tv_msg"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layouT_Below="@id/img_msg"
          android:layout_centerHorizontal="true"
          android:text="消息"
          android:textColor="@color/bottom_text_color_normal"
          android:textSize="12sp" />
      </RelativeLayout>
    </RelativeLayout>
    >

    <RelativeLayout

      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="1">

      <RelativeLayout
        android:id="@+id/layout_usercenter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true">

        <ImageView
          android:id="@+id/img_usercenter"
          android:layout_width="24dp"
          android:layout_height="24dp"
          android:layout_centerHorizontal="true"
          android:src="@mipmap/icon_user_normal" />

        <TextView
          android:id="@+id/tv_usercenter"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layouT_Below="@id/img_usercenter"
          android:layout_centerHorizontal="true"
          android:text="我的"
          android:textColor="@color/bottom_text_color_normal"
          android:textSize="12sp" />
      </RelativeLayout>
    </RelativeLayout>
    >
  </LinearLayout>

</LinearLayout>

activity_main.xml@H_801_2@

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://scheR_907_11845@as.android.com/apk/res/android"
  android:id="@+id/activity_main"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  >
  <FrameLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:id="@+id/frame_content"
    />
  <include layout="@layout/layouT_Bottom"/>

</LinearLayout>

@H_528_1@mainActivity@H_801_2@

public class MainActivity extends BaseActivity implements View.onClickListener {
  private RelativeLayout layout_home;
  private RelativeLayout layout_health;
  private RelativeLayout layout_msg;
  private RelativeLayout layout_usercenter;

  private ImageView img_home;
  private ImageView img_health;
  private ImageView img_msg;
  private ImageView img_usercenter;

  private TextView tv_home;
  private TextView tv_health;
  private TextView tv_msg;
  private TextView tv_usercenter;

  private Fragment[] fragments;
  privatE int currenTindex;
  privatE int index;

  privatE int SELEctColor;
  privatE int unSELEctColor;



  @Override
  protected void onCreate(Bundle savedInstanceStatE) {
    super.onCreate(savedInstanceStatE);
    setContentView(R.layout.activity_main);
    init();
  }

  @Override
  public void init() {
    initViews();
    initEvent();
    initData();

  }

  public void initViews(){
    layout_health=(RelativeLayout)findViewById(R.id.layout_health);
    layout_home=(RelativeLayout)findViewById(R.id.layout_homE);
    layout_msg=(RelativeLayout)findViewById(R.id.layout_msg);
    layout_usercenter=(RelativeLayout)findViewById(R.id.layout_usercenter);

    img_home=(ImageView)findViewById(R.id.img_homE);
    img_health=(ImageView)findViewById(R.id.img_health);
    img_msg=(ImageView)findViewById(R.id.img_msg);
    img_usercenter=(ImageView)findViewById(R.id.img_usercenter);

    tv_home=(TextView)findViewById(R.id.tv_homE);
    tv_health=(TextView)findViewById(R.id.tv_health);
    tv_msg=(TextView)findViewById(R.id.tv_msg);
    tv_usercenter=(TextView)findViewById(R.id.tv_usercenter);
  }

  public void initEvent(){
    layout_home.setOnClickListener(this);
    layout_health.setOnClickListener(this);
    layout_msg.setOnClickListener(this);
    layout_usercenter.setOnClickListener(this);
  }

  public void initData(){
    SELEctColor=getresources().getColor(R.color.bottom_text_color_pressed);
    unSELEctColor=getresources().getColor(R.color.bottom_text_color_normal);

    fragments=new Fragment[4];
    fragments[0]= HomeFragment.geTinstance();
    fragments[1]= HealthFragment.geTinstance();
    fragments[2]= MsgFragment.geTinstance();
    fragments[3]= UserCenterFragment.geTinstance();
    getSupportFragmentManager().begintransaction().add(R.id.frame_content,fragments[0]).commit();
  }

  @Override
  public void onClick(View view) {
    switch (view.getId()){
      case R.id.layout_home:
        index=0;
        setTabs(indeX);
        break;
      case R.id.layout_health:
        index=1;
        setTabs(indeX);
        break;
      case R.id.layout_msg:
        index=2;
        setTabs(indeX);
        break;
      case R.id.layout_usercenter:
        index=3;
        setTabs(indeX);
        break;
    }
    if(currenTindex!=indeX){
      FragmentManager fm=getSupportFragmentManager();
      Fragmenttransaction ft=fm.begintransaction();
      ft.hide(fragments[currenTindex]);
      if(!fragments[index].isAdded()){
        ft.add(R.id.frame_content,fragments[index]);
      }
      ft.show(fragments[index]).commit();
    }
    currenTindex=index;
  }

  public void setTabs(int pos){
    resetColor();
    switch (pos){
      case 0:
        img_home.setImageresource(R.mipmap.icon_home_pressed);
        tv_home.setTextColor(SELEctColor);
        break;
      case 1:
        img_health.setImageresource(R.mipmap.icon_health_pressed);
        tv_health.setTextColor(SELEctColor);
        break;
      case 2:
        img_msg.setImageresource(R.mipmap.icon_msg_pressed);
        tv_msg.setTextColor(SELEctColor);
        break;
      case 3:
        img_usercenter.setImageresource(R.mipmap.icon_user_pressed);
        tv_usercenter.setTextColor(SELEctColor);
        break;
    }

  }
  public void resetColor(){
    img_home.setImageresource(R.mipmap.icon_home_normal);
    img_health.setImageresource(R.mipmap.icon_health_normal);
    img_msg.setImageresource(R.mipmap.icon_msg_normal);
    img_usercenter.setImageresource(R.mipmap.icon_user_normal);
    tv_home.setTextColor(unSELEctColor);
    tv_health.setTextColor(unSELEctColor);
    tv_msg.setTextColor(unSELEctColor);
    tv_usercenter.setTextColor(unSELEctColor);

  }

}

四个Fragment只是一个简单的布局,里面放置了一个TextView,这里就不再贴出来o.o.

效果图预览:

Android Activity与Fragment实现底部导航器


效果

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持

大佬总结

以上是大佬教程为你收集整理的Android Activity与Fragment实现底部导航器全部内容,希望文章能够帮你解决Android Activity与Fragment实现底部导航器所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: