一.作业目标:App门户页面设计与开发,请根据课程内容设计一个app的门户框架,需要实现3-4个tab切换效果;本功能要求需要的技术为:activity、xml、fragment
二.技术说明:1.制作top.xml
代码
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@color/cardview_dark_background" android:gravity="center" android:text="@string/app_name" android:textColor="@color/white" android:textSize="35sp" /> </LinearLayout>
2,制作bottom.xml
代码(一部分)
android:textSize="25sp" /> </LinearLayout> <LinearLayout android:id="@+id/LinearLayout4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical"> <ImageView android:id="@+id/imageView4" android:layout_width="match_parent" android:layout_height="wrap_content" app:srcCompat="@android:drawable/btn_star_big_on" /> <TextView android:id="@+id/textView4" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:text="我的" android:textSize="25sp" /> </LinearLayout> </LinearLayout>
3.写个fragment Java文件及相应的tab.xml文件
因为实现3-4个tab切换效果,所以创建4个fragment java文件。
代码(fragment3)
package com.example.work2; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import androidx.fragment.app.Fragment; public class Fragment3 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.tab03, container, false); } }
4.写入activity.main.xml中
<?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"
tools:context=".MainActivity">
<include
android:id="@+id/include1"
layout="@layout/top"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
5.最终对mian文件进行编写
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.LinearLayout;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Fragment fragment1,fragment2,fragment3,fragment4; private FragmentManager manager; private FragmentTransaction transaction;
三.运行截图
代码库:林耀智/移动开发技术