一、设计目标
根据课程教学内容完成类微信的门户页面框架设计,APP最少必须包含4个tab页面。框架设计需要使用fragment,activity,不得使用UNIAPP技术进行开发(H5或者小程序)。
二、功能说明
图片的顶端是一个LinearLayout,它包含了一个TextVeiw文本框,用于显示软件名“xx”。
图片的中间是APP的四个tab页面,每个tab页面都是使用Fragment实现了activity与tab对应的页面分离,通过点击tab来切换页面,每个页面都有自己单独的文本显示内容。
图片的底端是由一些LinearLayout组合而成的导航栏,它显示的是四个主要功能和相对应的功能图标,当点击相对应的图标或者文字的时候,文字和图标会高亮显示提示你点击了某个相关的功能,当你点击另外的功能时,高亮也会随之刷新。
三、代码解析
1.顶部layout_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="70dp"
android:background="#D3D3D3">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="xx"
android:textSize="30sp" />
</LinearLayout>
2.底部layout_bottom.xml?
底部导航栏的实现主要是依靠多个LinearLayout组合而成的,首先是在底端放置了一个水平的LinearLayout,然后在其中放置四个垂直的LinearLayout,每个垂直的LinearLayout用于存放对应的文字TextView与图标ImageView。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="#D3D3D3"
android:gravity="center">
<LinearLayout
android:id="@+id/chat"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
app:srcCompat="@drawable/chat" />
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="消息"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/contact"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
app:srcCompat="@drawable/contact" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="通讯录"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/find"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
app:srcCompat="@drawable/find" />
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="发现"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/me"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
app:srcCompat="@drawable/me" />
<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="我 "
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
3.四个页面的Fragment.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">
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:gravity="center"
android:text="消息界面"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
4.activity_main.xml?
?在其中include顶部与底部的xml文件名。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context=".MainActivity">
<include layout="@layout/layout_top"></include>
<FrameLayout
android:id="@+id/frame_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"></FrameLayout>
<include layout="@layout/layout_bottom"></include>
</LinearLayout>
5.Fragment_chat.java(仅展示其一,其余与其类似)
package com.example.myapplication;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
public class Fragment_chat extends Fragment {
public Fragment_chat() {
}
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.layout_chat,container,false);
}
}
6.MainActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Fragment fragment_first=new Fragment_chat();
private Fragment fragment_second=new Fragment_contact();
private Fragment fragment_third=new Fragment_find();
private Fragment fragment_fourth=new Fragment_me();
private FragmentManager fragmentManager;
private LinearLayout linear_first;
private LinearLayout linear_second;
private LinearLayout linear_third;
private LinearLayout linear_fourth;
private ImageView imageView_first;
private ImageView imageView_second;
private ImageView imageView_third;
private ImageView imageView_fourth;
private TextView textView_first;
private TextView textView_second;
private TextView textView_third;
private TextView textView_fourth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE );
setContentView(R.layout.activity_main);
initView();
initFragment();
initEvent();
selectFragment(0);
imageView_first.setColorFilter(Color.CYAN);
textView_first.setTextColor(Color.CYAN);
}
@Override
public void onClick(View view) {
restartButton();
switch(view.getId())
{
case R.id.chat:
selectFragment(0);
imageView_first.setColorFilter(Color.CYAN);
textView_first.setTextColor(Color.CYAN);
break;
case R.id.contact:
selectFragment(1);
imageView_second.setColorFilter(Color.CYAN);
textView_second.setTextColor(Color.CYAN);
break;
case R.id.find:
selectFragment(2);
imageView_third.setColorFilter(Color.CYAN);
textView_third.setTextColor(Color.CYAN);
break;
case R.id.me:
selectFragment(3);
imageView_fourth.setColorFilter(Color.CYAN);
textView_fourth.setTextColor(Color.CYAN);
break;
default:
break;
}
}
private void restartButton() {
imageView_first.setColorFilter(0);
textView_first.setTextColor(Color.GRAY);
imageView_second.setColorFilter(0);
textView_second.setTextColor(Color.GRAY);
imageView_third.setColorFilter(0);
textView_third.setTextColor(Color.GRAY);
imageView_fourth.setColorFilter(0);
textView_fourth.setTextColor(Color.GRAY);
}
private void initFragment(){
fragmentManager=getSupportFragmentManager();
FragmentTransaction transaction=fragmentManager.beginTransaction();
transaction.add(R.id.frame_content,fragment_first);
transaction.add(R.id.frame_content,fragment_second);
transaction.add(R.id.frame_content,fragment_third);
transaction.add(R.id.frame_content,fragment_fourth);
transaction.commit();
}
private void initView(){
linear_first=findViewById(R.id.chat);
linear_second=findViewById(R.id.contact);
linear_third=findViewById(R.id.find);
linear_fourth=findViewById(R.id.me);
imageView_first=findViewById(R.id.imageView1);
imageView_second=findViewById(R.id.imageView2);
imageView_third=findViewById(R.id.imageView3);
imageView_fourth=findViewById(R.id.imageView4);
textView_first=findViewById(R.id.textView1);
textView_second=findViewById(R.id.textView2);
textView_third=findViewById(R.id.textView3);
textView_fourth=findViewById(R.id.textView4);
}
private void initEvent(){
linear_first.setOnClickListener(this);
linear_second.setOnClickListener(this);
linear_third.setOnClickListener(this);
linear_fourth.setOnClickListener(this);
}
private void hideView(FragmentTransaction transaction){
transaction.hide(fragment_first);
transaction.hide(fragment_second);
transaction.hide(fragment_third);
transaction.hide(fragment_fourth);
}
private void selectFragment(int i){
FragmentTransaction transaction=fragmentManager.beginTransaction();
hideView(transaction);
switch (i){
case 0:
transaction.show(fragment_first);
break;
case 1:
transaction.show(fragment_second);
break;
case 2:
transaction.show(fragment_third);
break;
case 3:
transaction.show(fragment_fourth);
break;
default:
break;
}
transaction.commit();
}
}
四、运行展示截图
五、源码仓库地址
GitHub - banyue2020/My_Application
|