设计目标
利用fragment,activity实现微信页面导航栏的功能,即能够通过点击导航栏实现页面的切换
功能说明
设置四个页面,通过switch-case进行页面的切换
代码解析
activity_main.xml文件,定义一个fragment
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="center"
tools:context=".MainActivity">
<fragment
android:id="@+id/fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.example.wechat.wechat_fragment"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="@+id/fragment"
tools:ignore="MissingConstraints">
<ImageView
android:id="@+id/image1"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:src="@drawable/wechat"
tools:ignore="MissingConstraints" />
<ImageView
android:id="@+id/image2"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:src="@drawable/message2"
tools:ignore="MissingConstraints" />
<ImageView
android:id="@+id/image3"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:src="@drawable/discovery"
tools:ignore="MissingConstraints" />
<ImageView
android:id="@+id/image4"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:src="@drawable/wode"
tools:ignore="MissingConstraints" />
</LinearLayout>
四个tab页面(图为发现页面代码,其他页面类似)
<?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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="发现"
android:textColor="#009688"
android:textSize="50sp" />
</RelativeLayout>
main activity
public class MainActivity extends AppCompatActivity {
private ViewPager mViewPager;
private List<View> mViews; //存放视图的数组
private View view1,view2,view3,view4;
private PagerAdapter mPagerAdapter;//适配器
@SuppressLint("InflateParams")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView1=findViewById(R.id.image1);
ImageView imageView2=findViewById(R.id.image2);
ImageView imageView3=findViewById(R.id.image3);
ImageView imageView4=findViewById(R.id.image4);
imageView1.setOnClickListener(l);
imageView2.setOnClickListener(l);
imageView3.setOnClickListener(l);
imageView4.setOnClickListener(l);
}
View.OnClickListener l=new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fm;
fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Fragment f;
f = null;
switch (v.getId()){
case R.id.image1:
f=new wechat_fragment();
break;
case R.id.image2:
f=new message_fragment();
break;
case R.id.image3:
f=new find_fragment();
break;
case R.id.image4:
f=new me_fragment();
break;
default:break;
}
ft.replace(R.id.fragment,f);
ft.commit();
}
};
运行展示截图
点击微信可以看到
点击 通讯录可以看到
点击发现可以看到
点击我的页面