主要效果:
主要文件:(最主要的也就activity_mail.xml和MainActivity两个)
其中,activity_mail.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"
tools:context=".MainActivity">
<fragment
android:id="@+id/fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.example.daohang.F_ym1"
/>
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<!--增加一个线性横向布局,在页面底部放置功能菜单(共5个按钮)-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
>
<ImageView
android:id="@+id/imageview1"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:src="@mipmap/tb1"/>
<ImageView
android:id="@+id/imageview2"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:src="@mipmap/tb2"/>
<ImageView
android:id="@+id/imageview3"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:src="@mipmap/tb3"/>
<ImageView
android:id="@+id/imageview4"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:src="@mipmap/tb4"/>
<ImageView
android:id="@+id/imageview5"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:src="@mipmap/tb5"/>
</LinearLayout>
</RelativeLayout>
MainActivity的代码:
package com.example.daohang;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
@Override/**/
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*生成变量绑定控件*/
ImageView iv1=(ImageView) findViewById(R.id.imageview1);
ImageView iv2=(ImageView) findViewById(R.id.imageview2);
ImageView iv3=(ImageView) findViewById(R.id.imageview3);
ImageView iv4=(ImageView) findViewById(R.id.imageview4);
ImageView iv5=(ImageView) findViewById(R.id.imageview5);
/*为变量绑定事件*/
iv1.setOnClickListener(l);
iv2.setOnClickListener(l);
iv3.setOnClickListener(l);
iv4.setOnClickListener(l);
iv5.setOnClickListener(l);
}
/*重写OnClickListener*/
View.OnClickListener l=new View.OnClickListener() {
@Override
/*
* 通过getSupportFragmentManager方法,生成Fragment管理器fm,
* 通过fm的beginTransaction方法,生成Fragment的操作员ft
* 生成Fragment对象f
* 通过判断点击的是哪个imageview,将对应的页面赋值给f
* ft将F插入到主页面的fragment中
* */
public void onClick(View v) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
Fragment f=null;
switch (v.getId()){
case R.id.imageview1:
f=new F_ym1();
break;
case R.id.imageview2:
f=new F_ym2();
break;
case R.id.imageview3:
f=new F_ym3();
break;
case R.id.imageview4:
f=new F_ym4();
break;
case R.id.imageview5:
f=new F_ym5();
break;
default:
break;
}
ft.replace(R.id.fragment,f);/*ft将F插入到主页面的fragment中*/
ft.commit();/*执行语句*/
}
};
}
其他参考代码:
<?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="match_parent"
android:text="这是页面1"
android:gravity="center"
/>
</RelativeLayout>
package com.example.daohang;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
/*新建F_ym1类继承Fragment*/
public class F_ym1 extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.f_ym1,null);
return view;
}
}