一.先看效果
二.长话短说,直接上代码。
activity_main.xml示例代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="#FFFFFF"
android:orientation="vertical"
tools:context=".MainActivity">
<!--Fragment作为容器,最为重要,其他代码可以不用分析,都是常规操作。-->
<FrameLayout
android:id="@+id/fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/view1"
/>
<View
android:id="@+id/view1"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_above="@+id/tab"
android:background="#DFE2EB" />
<RelativeLayout
android:id="@+id/tab"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#FFFFFF"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/tab1"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentStart="true"
android:layout_marginStart="20dp">
<ImageView
android:layout_marginTop="5dp"
android:layout_width="25dp"
android:layout_height="25dp"
android:background="@mipmap/home1"
android:layout_gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
android:text="首页"
android:textStyle="bold"
android:textColor="@color/black"
android:gravity="center"
android:layout_gravity="center"/>
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_toEndOf="@+id/tab1">
<ImageView
android:layout_marginTop="5dp"
android:layout_width="25dp"
android:layout_height="25dp"
android:background="@mipmap/teach1"
android:layout_gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
android:text="培训"
android:textColor="@color/black"
android:textStyle="bold"
android:gravity="center"
android:layout_gravity="center"/>
</LinearLayout>
<LinearLayout
android:id="@+id/tab3"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_toStartOf="@+id/tab4"
>
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:background="@mipmap/new1"
android:layout_marginTop="5dp"
android:layout_gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
android:text="新闻"
android:textStyle="bold"
android:textColor="@color/black"
android:gravity="center"
android:layout_gravity="center"/>
</LinearLayout>
<LinearLayout
android:id="@+id/tab4"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentEnd="true"
android:layout_marginEnd="20dp">
<ImageView
android:layout_marginTop="5dp"
android:layout_width="25dp"
android:layout_height="25dp"
android:background="@mipmap/me1"
android:layout_gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
android:text="我的"
android:textColor="@color/black"
android:textStyle="bold"
android:gravity="center"
android:layout_gravity="center"/>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/home_help"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:layout_marginBottom="10dp"
android:background="@drawable/help"
android:onClick="homeHelp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:layout_marginTop="10dp"
android:text="呼叫"
android:textColor="@color/white"
android:textSize="10sp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:layout_marginBottom="10dp"
android:text="120"
android:textColor="@color/white"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>
</RelativeLayout>
fragment_blank1.xml示例代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BlankFragment1"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_centerInParent="true"
android:text="fragment_blank1"
android:textSize="15sp" />
</LinearLayout>
fragment_blank2.xml示例代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BlankFragment1"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_centerInParent="true"
android:text="fragment_blank2"
android:textSize="15sp" />
</LinearLayout>
其他两个fragment_blank内容一样,省略。。。。。。。。
MainActivity.java示例代码:
package com.example.savebytheside;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Layout;
import android.view.View;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private BlankFragment1 fragment1;
private BlankFragment2 fragment2;
private BlankFragment3 fragment3;
private BlankFragment4 fragment4;
private FragmentManager fragmentManager;
private Fragment currentFragment;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragment1=new BlankFragment1();
fragment2=new BlankFragment2();
fragment3=new BlankFragment3();
fragment4=new BlankFragment4();
fragmentManager =getSupportFragmentManager();
//默认显示第一个fragment
fragmentManager.beginTransaction().add(R.id.fragment,fragment1).commit();
currentFragment=fragment1;
findViewById(R.id.tab1).setOnClickListener(this);
findViewById(R.id.tab2).setOnClickListener(this);
findViewById(R.id.tab3).setOnClickListener(this);
findViewById(R.id.tab4).setOnClickListener(this);
// findViewById(R.id.home_help).setOnClickListener(this);
}
@Override
public void onClick(View view) {
Fragment targetFragment=null;
switch(view.getId()){
case R.id.tab1:
targetFragment=fragment1;
break;
case R.id.tab2:
targetFragment=fragment2;
break;
case R.id.tab3:
targetFragment=fragment3;
break;
case R.id.tab4:
targetFragment=fragment4;
break;
}
// 判断当前选择的 Fragment 是否与目标 Fragment 相同,如果相同则不处理
if (currentFragment != targetFragment) {
FragmentTransaction transaction = fragmentManager.beginTransaction();
if (!targetFragment.isAdded()) {
transaction.add(R.id.fragment, targetFragment);
}
transaction.hide(currentFragment).show(targetFragment).commit();
currentFragment = targetFragment;
}
}
public void homeHelp(View view){
Intent intent=new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+"120"));
startActivity(intent);
}
}
注意:其余四个fragment_blank相关的BlankFragment.java文件,如果不设置内容则其代码可以默认不动。