一.设计目标
在Android studio 上开发一个简单的微信界面,并实现切换效果
二.功能说明
设计好的微信界面共有四个页面,点击不同按钮后,显示四个不同的界面
三.代码解析
先分别创建两个布局文件bottom.xml、top.xml用于展示页面的顶部和底部,中间用一个framelayout填充,用四个fragement来展示四个tab页面,并实现四个页面之间的点击切换,且能够通过监听控制实现在点击不同控件时能展示出对应的tab页面。
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="80dp"
android:gravity="center"
android:background="#3C3737"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="WeChat"
android:textColor="#ffffff"
android:textSize="20sp" />
</LinearLayout>
效果图
2.buttom.xml设计
在一级linearlayout下再放置4个linearlayout,每个二级linearlayout下分别再放置一个imageview和一个textview用于填充相应的微信图标和文字
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_gravity="bottom"
android:baselineAligned="false">
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:contentDescription="TODO"
app:srcCompat="@drawable/xiaoxi" />
<TextView
android:id="@+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="消息"
android:textSize="25sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="@drawable/tongxun" />
<TextView
android:id="@+id/contact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="通讯录"
android:textSize="25sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="@drawable/faxian" />
<TextView
android:id="@+id/find"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="发现"
android:textSize="25sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout4"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="@drawable/me" />
<TextView
android:id="@+id/config"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="我"
android:textSize="25sp" />
</LinearLayout>
</LinearLayout>
3.在mainactivity.xml中加入一个framelayout用于展示页面信息,下面是相关代码
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/top"></include>
<FrameLayout
android:id="@+id/id_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
</FrameLayout>
<include layout="@layout/bottom"></include>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
四个fragment的代码如下,其格式基本一致
MessageFragement.java代码如下:
package com.example.practice1;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MessageFragment extends Fragment {
public MessageFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_message, container, false);
}
}
fragement_message.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".MessageFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/messageview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="这是消息界面"
android:gravity="center"
android:textSize="30sp"/>
</FrameLayout>
CommunicationFragement.java代码如下:
package com.example.practice1;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
*/
public class CommunicationFragment extends Fragment {
public CommunicationFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_communication, container, false);
}
}
fragement_communication.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".CommunicationFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/messageview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="这是通讯界面"
android:gravity="center"
android:textSize="30sp"/>
</FrameLayout>
FindFragement.java代码如下:
package com.example.practice1;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FindFragment extends Fragment {
public FindFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_find, container, false);
}
}
fragement_find.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".FindFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/messageview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="这是发现界面"
android:gravity="center"
android:textSize="30sp"/>
—
</FrameLayout>
MeFragement.java代码如下:
package com.example.practice1;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
*/
public class MeFragment extends Fragment {
public MeFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_me, container, false);
}
}
fragement_me.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".MeFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/messageview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="这是本地用户界面"
android:gravity="center"
android:textSize="30sp"/>
</FrameLayout>
MainActivity.java代码:
package com.example.practice1;
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.SurfaceControl;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private LinearLayout linearLayout1,linearLayout2,linearLayout3,linearLayout4;
private FragmentManager fragmentManager;
private FragmentTransaction fragmentTransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initEvent();
linearLayout1.setBackgroundColor(Color.rgb(155,155,155));
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.id_content, new MessageFragment());
fragmentTransaction.commit();
}
private void initEvent(){
linearLayout1.setOnClickListener(this);
linearLayout2.setOnClickListener(this);
linearLayout3.setOnClickListener(this);
linearLayout4.setOnClickListener(this);
}
private void initView(){
linearLayout1 = findViewById(R.id.linearLayout1);
linearLayout2 = findViewById(R.id.linearLayout2);
linearLayout3 = findViewById(R.id.linearLayout3);
linearLayout4 = findViewById(R.id.linearLayout4);
}
@Override
public void onClick(View view){
setAllColor();
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
switch (view.getId()){
case R.id.linearLayout1:
fragmentTransaction.replace(R.id.id_content,new MessageFragment());
linearLayout1.setBackgroundColor(Color.rgb(155,155,155));
break;
case R.id.linearLayout2:
fragmentTransaction.replace(R.id.id_content,new CommunicationFragment());
linearLayout2.setBackgroundColor(Color.rgb(155,155,155));
break;
case R.id.linearLayout3:
fragmentTransaction.replace(R.id.id_content,new FindFragment());
linearLayout3.setBackgroundColor(Color.rgb(155,155,155));
break;
case R.id.linearLayout4:
fragmentTransaction.replace(R.id.id_content,new MeFragment());
linearLayout4.setBackgroundColor(Color.rgb(155,155,155));
break;
}
fragmentTransaction.commit();
}
private void setAllColor() {
linearLayout1.setBackgroundColor(Color.WHITE);
linearLayout2.setBackgroundColor(Color.WHITE);
linearLayout3.setBackgroundColor(Color.WHITE);
linearLayout4.setBackgroundColor(Color.WHITE);
}
}
四.运行截图
点击后相应图标变成深色,并显示相应的页面
五.实验小结
本次实验后,我学会了安卓软件中的一些基本控件的使用以及相关设置,在微信页面的设计之中,认识到fragment和activity的使用技巧,以及页面切换如何实现。在实验中要注意一些语言的特殊格式,避免出现语法错误,以后更要勤加练习,熟悉其他的用法和功能。