安卓实现底部tab栏

109 阅读1分钟

image.png

实现代码如下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/color_page_bg">


    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/white"
        android:orientation="horizontal"
        android:paddingTop="9dp"
        android:paddingBottom="2dp">

        <RelativeLayout
            android:id="@+id/home_layout_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1">

            <ImageView
                android:id="@+id/home_image_view"
                android:layout_width="21dp"
                android:layout_height="21dp"
                android:layout_centerHorizontal="true"
                android:src="@drawable/home_choose" />

            <TextView
                android:id="@+id/home_text_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/home_image_view"
                android:layout_centerHorizontal="true"
                android:text="Home"
                android:textColor="@color/color_choose"
                android:textSize="11sp" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/service_layout_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1">

            <ImageView
                android:id="@+id/message_image_view"
                android:layout_width="21dp"
                android:layout_height="21dp"
                android:layout_centerHorizontal="true"
                android:src="@drawable/service_unchoose" />

            <TextView
                android:id="@+id/message_text_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/message_image_view"
                android:layout_centerHorizontal="true"
                android:text="Service"
                android:textColor="@color/color_unchoose"
                android:textSize="11sp" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/mine_layout_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1">

            <ImageView
                android:id="@+id/mine_image_view"
                android:layout_width="21dp"
                android:layout_height="21dp"
                android:layout_centerHorizontal="true"
                android:src="@drawable/my_unchoose" />

            <TextView
                android:id="@+id/mine_text_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/mine_image_view"
                android:layout_centerHorizontal="true"
                android:text="My"
                android:textColor="@color/color_unchoose"
                android:textSize="11sp" />
        </RelativeLayout>
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/content_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/linearLayout" />

    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:layout_above="@+id/linearLayout"
        android:background="@color/color_choose" />

</RelativeLayout>

package com.example.lby;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.graphics.Color; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.Window; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView;

import com.example.lby.view.fragment.HomeFragment; import com.example.lby.view.fragment.MyFragment; import com.example.lby.view.fragment.ServiceFragment;

public class MainActivity extends AppCompatActivity implements View.OnClickListener { public String TAG="wcs";

public HomeFragment homeFragment=null;
public ServiceFragment serviceFragment=null;
public MyFragment myFragment=null;

public   FragmentManager mFragmentManager=null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //设置状态栏主题
    setStatusColor(this);
    initView();
}

/**
 * 初始化界面
 * **/
private void initView() {
    RelativeLayout rHome= findViewById(R.id.home_layout_view);
    RelativeLayout rService= findViewById(R.id.service_layout_view);
    RelativeLayout rMy= findViewById(R.id.mine_layout_view);

    rHome.setOnClickListener(this);
    rService.setOnClickListener(this);
    rMy.setOnClickListener(this);

    //创建局部的碎片 Fragment
    homeFragment= new HomeFragment();
    //需要有个 碎片管理 事务 开始 事务结束 提交
    mFragmentManager= getFragmentManager();
    FragmentTransaction mFragmentTransaction= mFragmentManager.beginTransaction();
    mFragmentTransaction.replace(R.id.content_layout,homeFragment);
    mFragmentTransaction.commit();
}

private void setStatusColor(Activity activity) {
    Window window = activity.getWindow();
    window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}

@Override
public void onClick(View view) {
  int positionID=view.getId();
    ImageView imgView= findViewById(R.id.home_image_view);
    TextView tvView= findViewById(R.id.home_text_view);
    ImageView msgImg= findViewById(R.id.message_image_view);
    TextView msgTx= findViewById(R.id.message_text_view);
    ImageView mingImg= findViewById(R.id.mine_image_view);
    TextView myView= findViewById(R.id.mine_text_view);

    FragmentTransaction mFragmentTransaction= mFragmentManager.beginTransaction();
  if(positionID==R.id.home_layout_view){
      Log.d(TAG, "onClick: home 被点击了");
      // 移除选中颜色 和图片 其他的颜色设置为 灰色
      imgView.setImageResource(R.drawable.home_choose);
      tvView.setTextColor( getResources().getColor(R.color.color_choose));
      msgImg.setImageResource(R.drawable.service_unchoose);
      msgTx.setTextColor( getResources().getColor(R.color.color_unchoose));
      mingImg.setImageResource(R.drawable.my_unchoose);
      myView.setTextColor( getResources().getColor(R.color.color_unchoose));

      if(serviceFragment!=null){
          mFragmentTransaction.hide(serviceFragment);
      }
      if(myFragment!=null){
          mFragmentTransaction.hide(myFragment);
      }
      if(homeFragment==null){
          //创建局部的碎片 Fragment
          homeFragment= new HomeFragment();
          //需要有个 碎片管理 事务 开始 事务结束 提交
          mFragmentTransaction.add(R.id.content_layout,homeFragment);
      }else{
          mFragmentTransaction.show(homeFragment);
      }
      mFragmentTransaction.commit();
  }
  if(positionID==R.id.service_layout_view){
      Log.d(TAG, "onClick: service 被点击了");
      imgView.setImageResource(R.drawable.home_unchoose);
      tvView.setTextColor( getResources().getColor(R.color.color_unchoose));
      msgImg.setImageResource(R.drawable.service_choose);
      msgTx.setTextColor( getResources().getColor(R.color.color_choose));
      mingImg.setImageResource(R.drawable.my_unchoose);
      myView.setTextColor( getResources().getColor(R.color.color_unchoose));
      if(homeFragment!=null){
          mFragmentTransaction.hide(homeFragment);
      }
      if(myFragment!=null){
          mFragmentTransaction.hide(myFragment);
      }
      if(serviceFragment==null){
          //创建局部的碎片 Fragment
          serviceFragment= new ServiceFragment();
          //需要有个 碎片管理 事务 开始 事务结束 提交
          mFragmentTransaction.add(R.id.content_layout,serviceFragment);
      }else{
          mFragmentTransaction.show(serviceFragment);
      }
      mFragmentTransaction.commit();
  }
  if(positionID==R.id.mine_layout_view){
      Log.d(TAG, "onClick: my 被点击了");
      imgView.setImageResource(R.drawable.home_unchoose);
      tvView.setTextColor( getResources().getColor(R.color.color_unchoose));
      msgImg.setImageResource(R.drawable.service_unchoose);
      msgTx.setTextColor( getResources().getColor(R.color.color_unchoose));
      mingImg.setImageResource(R.drawable.my_choose);
      myView.setTextColor( getResources().getColor(R.color.color_choose));
      if(serviceFragment!=null){
          mFragmentTransaction.hide(serviceFragment);
      }
      if(homeFragment!=null){
          mFragmentTransaction.hide(homeFragment);
      }
      if(myFragment==null){
          //创建局部的碎片 Fragment
          myFragment= new MyFragment();
          //需要有个 碎片管理 事务 开始 事务结束 提交
          mFragmentTransaction.add(R.id.content_layout,myFragment);
      }else{
          mFragmentTransaction.show(myFragment);
      }
      mFragmentTransaction.commit();
  }
}

} 比较简单的一种实现方式