AS开发 简单的类微信界面
一、项目内容
进行类微信界面框架设计,包含微信、联系人、发现、设置四个tab页面,点击相应图标进行切换。
二、代码实现
1、项目结构
3.顶部和底部代码
使用LinearLayout布局
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="65dp"
android:gravity="center"
android:background="#000000"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:text="WeChat"
android:textColor="#ffffff"
android:textSize="40sp" />
</LinearLayout>

bottom.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"
android:layout_width="match_parent"
android:background="@drawable/bottom_bar"
android:layout_height="130dp"
android:baselineAligned="false">
<LinearLayout
android:id="@+id/id_tab_weixin"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<ImageButton
android:id="@+id/id_tab_WeChat_img"
android:layout_width="110dp"
android:layout_height="80dp"
android:background="#000000"
android:contentDescription="@string/app_name"
android:clickable="false"
android:scaleType="fitCenter"
app:srcCompat="@drawable/tab_weixin_pressed" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:clickable="false"
android:saveEnabled="false"
android:text="微信"
android:textColor="#ffffff"
android:textSize="15sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/id_tab_frd"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<ImageButton
android:clickable="false"
android:id="@+id/id_tab_frd_img"
android:layout_width="110dp"
android:layout_height="80dp"
android:background="#000000"
android:contentDescription="@string/app_name"
android:scaleType="fitCenter"
app:srcCompat="@drawable/tab_find_frd_normal" />
<TextView
android:clickable="false"
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:saveEnabled="false"
android:text="朋友"
android:textColor="#ffffff"
android:textSize="15sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/id_tab_contact"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<ImageButton
android:clickable="false"
android:id="@+id/id_tab_contact_img"
android:layout_width="110dp"
android:layout_height="80dp"
android:background="#000000"
android:contentDescription="@string/app_name"
android:scaleType="fitCenter"
app:srcCompat="@drawable/tab_address_normal" />
<TextView
android:clickable="false"
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:saveEnabled="false"
android:text="通讯录"
android:textColor="#ffffff"
android:textSize="15sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/id_tab_settings"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<ImageButton
android:clickable="false"
android:id="@+id/id_tab_settings_img"
android:layout_width="110dp"
android:layout_height="80dp"
android:background="#000000"
android:contentDescription="@string/app_name"
android:scaleType="fitCenter"
app:srcCompat="@drawable/tab_settings_normal" />
<TextView
android:clickable="false"
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:saveEnabled="false"
android:text="设置"
android:textColor="#ffffff"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>
4.四个内容界面
tab01.xml,tab02.xml,tab03.xml,tab04.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/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="25sp"
android:gravity="center"
android:layout_gravity="center"
android:text="这是聊天界面" />
</LinearLayout>

<?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/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="25sp"
android:gravity="center"
android:layout_gravity="center"
android:text="这是通讯录界面" />
</LinearLayout>
<?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/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="25sp"
android:gravity="center"
android:layout_gravity="center"
android:text="这是发现界面" />
</LinearLayout>

<?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/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="25sp"
android:gravity="center"
android:layout_gravity="center"
android:text="这是设置界面" />
</LinearLayout>

5.MainActivity点击图标显示对应页面
package com.example.mywechat;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Fragment mTab01 = new WeChat_BlankFragment();
private Fragment mTab02 = new friend_BlankFragment();
private Fragment mTab03 = new find_BlankFragment();
private Fragment mTab04 = new settings_BlankFragment();
private LinearLayout mTabWeChat;
private LinearLayout mTabFrd;
private LinearLayout mTabFind;
private LinearLayout mTabSettings;
private ImageButton mImgWeChat;
private ImageButton mImgFrd;
private ImageButton mImgFind;
private ImageButton mImgSettings;
private FragmentManager fm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getSupportActionBar() != null){
getSupportActionBar().hide();
}
setContentView(R.layout.activity_main);
initFragment();
initView();
initEvent();
selectFragment(0);
}
private void initFragment() {
fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.add(R.id.id_content, mTab01);
transaction.add(R.id.id_content, mTab02);
transaction.add(R.id.id_content, mTab03);
transaction.add(R.id.id_content, mTab04);
transaction.commit();
}
private void resetImgs() {
mImgWeChat.setImageResource(R.drawable.tab_weixin_normal);
mImgFrd.setImageResource(R.drawable.tab_find_frd_normal);
mImgFind.setImageResource(R.drawable.tab_address_normal);
mImgSettings.setImageResource(R.drawable.tab_settings_normal);
}
private void initView() {
mTabWeChat = (LinearLayout) findViewById(R.id.id_tab_weixin);
mTabFrd = (LinearLayout) findViewById(R.id.id_tab_frd);
mTabFind = (LinearLayout) findViewById(R.id.id_tab_contact);
mTabSettings = (LinearLayout) findViewById(R.id.id_tab_settings);
mImgWeChat = (ImageButton) findViewById(R.id.id_tab_WeChat_img);
mImgFrd = (ImageButton) findViewById(R.id.id_tab_frd_img);
mImgFind = (ImageButton) findViewById(R.id.id_tab_contact_img);
mImgSettings = (ImageButton) findViewById(R.id.id_tab_settings_img);
}
private void selectFragment(int i) {
FragmentTransaction transaction = fm.beginTransaction();
hideFragment(transaction);
switch (i) {
case 0:
Log.d("setSelect", "1");
transaction.show(mTab01);
mImgWeChat.setImageResource(R.drawable.tab_weixin_pressed);
break;
case 1:
transaction.show(mTab02);
mImgFrd.setImageResource(R.drawable.tab_find_frd_pressed);
break;
case 2:
transaction.show(mTab03);
mImgFind.setImageResource(R.drawable.tab_address_pressed);
break;
case 3:
transaction.show(mTab04);
mImgSettings.setImageResource(R.drawable.tab_settings_pressed);
break;
default:
break;
}
transaction.commit();
}
private void initEvent() {
mTabWeChat.setOnClickListener(this);
mTabFrd.setOnClickListener(this);
mTabFind.setOnClickListener(this);
mTabSettings.setOnClickListener(this);
}
private void hideFragment(FragmentTransaction transaction) {
transaction.hide(mTab01);
transaction.hide(mTab02);
transaction.hide(mTab03);
transaction.hide(mTab04);
}
@Override
public void onClick(View v) {
Log.d("onClick", "1");
resetImgs();
switch (v.getId()) {
case R.id.id_tab_weixin:
Log.d("onClick", "2");
selectFragment(0);
break;
case R.id.id_tab_frd:
selectFragment(1);
break;
case R.id.id_tab_contact:
selectFragment(2);
break;
case R.id.id_tab_settings:
selectFragment(3);
break;
default:
break;
}
}
}
6.运行效果
源码地址:https://gitee.com/cftmpxr/demo-of-pxr
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理