底部导航栏的实现
布局代码
<?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" > <LinearLayout android:id="@+id/tab_menu" android:layout_width="match_parent" android:layout_height="70dp" android:orientation="horizontal" android:layout_alignParentBottom="true"> <TextView android:id="@+id/txt_deal" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/tab_menu_bg" android:drawableTop="@drawable/tab_menu_deal" android:gravity="center" android:textColor="@drawable/tab_menu_deal_text" android:text="购买"/> <TextView android:id="@+id/txt_poi" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/tab_menu_bg" android:drawableTop="@drawable/tab_menu_poi" android:gravity="center" android:textColor="@drawable/tab_menu_deal_text" android:text="消息"/> <TextView android:id="@+id/txt_more" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/tab_menu_bg" android:drawableTop="@drawable/tab_menu_show" android:gravity="center" android:textColor="@drawable/tab_menu_deal_text" android:text="show"/> <TextView android:id="@+id/txt_user" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/tab_menu_bg" android:drawableTop="@drawable/tab_menu_user" android:gravity="center" android:textColor="@drawable/tab_menu_deal_text" android:text="我的"/> </LinearLayout> <View android:id="@+id/div_tab_bar" android:layout_width="match_parent" android:layout_height="2dp" android:background="#000000" android:layout_above="@id/tab_menu"/> <FrameLayout android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="0.01dp" android:layout_above="@id/tab_menu" android:background="#F2FBEF"> </FrameLayout> </RelativeLayout>
这里我的想法是用四个fragment的切换来实现
在HomeActivity中定义四个Fragment,首先通过transaction.hide把四个全部隐藏,然后设置点击事件,被点中的变成true然后显示
package com.example.expressdelivery; import android.os.Bundle; import android.view.View; import android.view.Window; import android.widget.FrameLayout; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.FragmentManager; import Home.BuyFragment; import Home.MessageFragment; import Home.MyFragment; import Home.ShowFragment; public class HomeActivity extends AppCompatActivity { private TextView topBar; private TextView tabDeal; private TextView tabPoi; private TextView tabMore; private TextView tabUser; private FrameLayout ly_content; private BuyFragment f1; private MessageFragment f2; private MyFragment f4; private ShowFragment f3; private FragmentManager fragmentManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_home); tabDeal = findViewById(R.id.txt_deal); tabPoi = findViewById(R.id.txt_poi); tabUser = findViewById(R.id.txt_user); tabMore =findViewById(R.id.txt_more); ly_content = findViewById(R.id.fragment_container); setListeners(); } private void setListeners() { OnClick onClick=new OnClick(); tabDeal.setOnClickListener(onClick); tabMore.setOnClickListener(onClick); tabUser.setOnClickListener(onClick); tabPoi.setOnClickListener(onClick); } public void selected(){ tabDeal.setSelected(false); tabMore.setSelected(false); tabPoi.setSelected(false); tabUser.setSelected(false); } //隐藏所有Fragment public void hideAllFragment( androidx.fragment.app.FragmentTransaction transaction){ if(f1!=null){ transaction.hide(f1); } if(f2!=null){ transaction.hide(f2); } if(f3!=null){ transaction.hide(f3); } if(f4!=null){ transaction.hide(f4); } } private class OnClick implements View.OnClickListener { public void onClick(View v) { androidx.fragment.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); hideAllFragment(transaction); switch (v.getId()) { case R.id.txt_deal: selected(); tabDeal.setSelected(true); if (f1 == null) { f1 = new BuyFragment(); transaction.add(R.id.fragment_container, f1); } else { transaction.show(f1); } break; case R.id.txt_poi: selected(); tabPoi.setSelected(true); if (f2 == null) { f2 = new MessageFragment(); transaction.add(R.id.fragment_container, f2); } else { transaction.show(f2); } break; case R.id.txt_more: selected(); tabMore.setSelected(true); if (f3 == null) { f3 = new ShowFragment(); transaction.add(R.id.fragment_container, f3); } else { transaction.show(f3); } break; case R.id.txt_user: selected(); tabUser.setSelected(true); if (f4 == null) { f4 = new MyFragment(); transaction.add(R.id.fragment_container, f4); } else { transaction.show(f4); } break; } transaction.commit(); } } }
然后建立四个fragment也就是购买、消息、我的、社区四个。