Android Studio安卓开发-类微信UI设计
新建一个安卓空项目,语言采用Java,基于Android SDK11.0实现,使用虚拟设备Pixel 5 API 30.
实现顶部微信栏-layout_top.xml.
创建时选择LinearLayout,新建一个TextView表示一个文本框,设置gravity为center,即实现居中,文本输入微信,大小设置为24sp.
<?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="wrap_content"><TextViewandroid:id="@+id/textView"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:layout_weight="1"android:gravity="center"android:text="微信"android:textSize="24sp" />
</LinearLayout>
![](/assets/blank.gif)
实现底部微信栏-layout_bottom.xml.
导入图片资源于resource目录下,用于实现微信底部菜单栏图片以及点击后图片变换效果。
![](/assets/blank.gif)
创建时选择LinearLayout,同时新建四个LinearLayout于父LinearLayout,四个LinearLayout各含一个TextView和一个imageButton,用于实现微信底部菜单栏。
![](/assets/blank.gif)
调整图片大小,设置背景,设置垂直布局,TextView文本居中,如果图片大小适合,较为适用的选择wrap_content,系统自动弹性布局高度,每个子LinearLayout的weight权重为1,此底部菜单栏需要不断调整以达到想要的效果,下面是其中一个LinearLayout的配置。
<LinearLayoutandroid:id="@+id/ChatLinear"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_weight="1"android:orientation="vertical"><ImageButtonandroid:id="@+id/imageButton2"android:layout_width="match_parent"android:layout_height="54dp"android:background="#ffffff"android:clickable="false"android:contentDescription="@string/androidx_startup"android:scaleType="centerInside"app:srcCompat="@drawable/chat" /><TextViewandroid:id="@+id/textView2"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center_horizontal"android:text="微信"android:textSize="20sp" />
</LinearLayout>
![](/assets/blank.gif)
创建Fragment.
在java目录下创建四个空Fragment,用于实现微信中间内容的展示与切换,需要注意命名,对应生成了四个Fragment_layout.xml,我们修改四个xml的TextView文本内容,分别提示用户聊天界面、联系人界面、发现界面、我的界面。下面是用户聊天界面的展示。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/layout1"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:id="@+id/textView6"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:textSize="40sp"android:text="这是聊天界面!" />
</LinearLayout>
![](/assets/blank.gif)
界面整合-activity_main.xml.
使用include标签将之前设计的top.xml和bottom.xml导入,高度选择wrap_content,宽度选择match_parent表示与父LinearLayout一致,中间加入一个FrameLayout,id为content,设置权值为1,为四个fragment_layout.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"android:baselineAligned="false"><includelayout="@layout/layout_top"android:layout_width="match_parent"android:layout_height="wrap_content" /><FrameLayoutandroid:id="@+id/content"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"></FrameLayout><includelayout="@layout/layout_bottom"android:layout_width="match_parent"android:layout_height="wrap_content" /></LinearLayout>
![](/assets/blank.gif)
编写MainActivity.
定义initFragment函数,实现Fragment初始化。
首先new四个Fragment私有成员对象,创建事务transaction,对应id为content,关联四个Fragment。
private void initFragment(){
fragmentManager = getSupportFragmentManager();FragmentTransaction transaction = fragmentManager.beginTransaction();transaction.add(R.id.content,chatFragment);transaction.add(R.id.content,contactsFragment);transaction.add(R.id.content,discoveryFragment);transaction.add(R.id.content,selfFragment);transaction.commit();
}
定义initView函数,实现视图初始化。
首先new四个LinearLayout、四个TextView、四个ImageButton私有成员对象,在函数中利用findViewById对应到四个LinearLayout线性布局、四个imageButton图片按钮、四个TextView文本图窗。
private void initView(){
chatLayout = findViewById(R.id.ChatLinear);contactsLayout = findViewById(R.id.ContactsLinear);discoveryLayout = findViewById(R.id.DiscoveryLinear);selfLayout = findViewById(R.id.SelfLinear);chatImageButton = findViewById(R.id.imageButton2);contactsImageButton = findViewById(R.id.imageButton3);discoveryImageButton = findViewById(R.id.imageButton4);selfImageButton = findViewById(R.id.imageButton5);chatText = findViewById(R.id.textView2);contactsText = findViewById(R.id.textView3);discoveryText= findViewById(R.id.textView4);selfText = findViewById(R.id.textView5);
}
定义hideFragment函数,隐藏四个Fragment。
private void hideFragment(FragmentTransaction transaction){transaction.hide(chatFragment);transaction.hide(contactsFragment);transaction.hide(discoveryFragment);transaction.hide(selfFragment);
}
定义选择器selectFragment函数,实现界面切换。
在这一步骤中,首先调用hideFrament,隐藏所有Fragment,使用switch-case语句,用于切换四个Fragment,在这一步骤中,我们实现图片资源的切换,当ImageButton被点击时,我们将图片切换为绿的,符合微信设置,同时将文字从黑变成绿色,符合微信主题。
@SuppressLint("ResourceAsColor")
private void selectFragment(int i){//i是一个选择器,用于选择显示哪个界面FragmentTransaction transaction = fragmentManager.beginTransaction();hideFragment(transaction);//首先隐藏所有页面switch (i){
case 0:transaction.show(chatFragment);chatImageButton.setImageResource(R.drawable.chat_pick);chatText.setTextColor(Color.GREEN);break;case 1:transaction.show(contactsFragment);contactsImageButton.setImageResource(R.drawable.friends_pick);contactsText.setTextColor(Color.GREEN);break;case 2:transaction.show(discoveryFragment);discoveryImageButton.setImageResource(R.drawable.comm_pick);discoveryText.setTextColor(Color.GREEN);break;case 3:transaction.show(selfFragment);selfImageButton.setImageResource(R.drawable.setting_pick);selfText.setTextColor(Color.GREEN);break;default:
break;}transaction.commit();
}
定义resetBtn函数,实现按钮重设。
在这一部分,主要是为了当切换微信菜单栏时,重设所有按钮,使图标不再变绿,切换图片资源,同时,将文本转换为黑色。
@SuppressLint("ResourceAsColor")
private void resetBtn(){
chatImageButton.setImageResource(R.drawable.chat);contactsImageButton.setImageResource(R.drawable.friends);discoveryImageButton.setImageResource(R.drawable.comm);selfImageButton.setImageResource(R.drawable.setting);chatText.setTextColor(Color.BLACK);contactsText.setTextColor(Color.BLACK);discoveryText.setTextColor(Color.BLACK);selfText.setTextColor(Color.BLACK);
}
定义initEvent函数,实现对bottom四个LinearLayout的监听。
在这一部分,需要记得在类implements View.OnClickListener提供接口,实现点击动作的监听。
private void initEvent(){
chatLayout.setOnClickListener(this);contactsLayout.setOnClickListener(this);discoveryLayout.setOnClickListener(this);selfLayout.setOnClickListener(this);
}
整体界面展示。
在类中重写onCreate方法,调用封包函数,运行项目,查看整体运行结果,默认界面是0,表示聊天界面。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();initEvent();initFragment();selectFragment(0);//默认首页是聊天界面
}
![](/assets/blank.gif)
![](/assets/blank.gif)
总结。
本次较为简单的实现了一个类微信UI的设计,体现大致的界面布局,对于微信的详细功能的实现,将在往后的博客中体现,吸取几点经验教训如下,为后人栽树:
不能忘记implements View.OnClickListener,提供视图的监听接口;
巧妙使用|管道符号,实现bottom底部和center_horizontal水平中心的同时设置;
setTextColor设置文本颜色,实现点击时,文本颜色的切换;
创建空Fragment时,会自动生成对应的layout.xml,不需要我们手动创建;
巧妙使用wrap_content让系统为你自动生成美观的布局;
switch-case语句记得使用break退出;
使用include标签,插入layout.xml,整合界面。
代码地址。
点击查看源码地址
Android Studio安卓开发-类微信UI设计相关推荐
- Android Studio安卓开发-RecycleView新闻栏设计
RecycleView新闻栏设计 绪言 1 修改Item_dome.xml文件 2 创建适配器Adapter3 2.1 编写构造方法传入数据 2.2 创建内部类,初始化Item 2.3 将数据和控件绑 ...
- Android studio 安卓开发常见问题(个人笔记系列)
Android studio安卓开发常见问题 注:个人笔记就是没有大纲.没有目录.没有结构,纯属本人笔记用. 正文 1.建议为控件绑定监听器而不是用XML的onClick属性来实现一个方法. 2.匿名 ...
- 学习笔记 | Android Studio安卓开发入门经验总结 干货
前言 最近完成了移动编程课程的学习,加上其它安卓开发项目的经历,感觉收获颇为丰富.故在此总结整理安卓开发中比较常见的一些问题,技巧和指南. 0.目录 文章目录 前言 0.目录 1.开发环境 2. 项目 ...
- Android Studio安卓开发中使用json来作为网络数据传输格式
如果你是在安卓开发中并且使用android studio,要使用json来作为数据传输的格式,那么下面是我的一些经验. 一开始我在android studio中导入那6个包,那6个包找了非常久,因为放 ...
- 【Android studio安卓开发】如何连接模拟器?以逍遥模拟器和夜神模拟器为例。
一.Android studio如何连接逍遥模拟器? 对于逍遥模拟器的连接,比较简单,直接启动逍遥模拟器,AS可以检测到模拟器已启动,直接点击选择运行即可. 我这里逍遥模拟器显示的是一加手机: 但是! ...
- 安卓开发之基本UI设计
视图 视图概念: 一个Activity的功能很多,但它本身无法显示在屏幕上,而是借助于视图组(ViewGroup)和视图(View),这两个才是最基本的用户界面表达单元. 一个View对象就是一个数据 ...
- java android studio 安卓开发 - 使用java发送邮件
01 先导包 https://javaee.github.io/javamail/#Samples 里面找到 for android的连接 但是后面run时,报错以下内容 javax.mail.Ses ...
- android studio 安卓新建类如何自动填充作者、时间、备注等信息
之前有实习生问我:你新建的类,怎么自动填充你姓名.联系方式.创建时间.作用这些的? 下面几步记录一下,给需要的朋友: 设置里面,file-->setting-->Editor-->f ...
- Android Studio——类微信界面设计
设计目标:Android studio制作简易类微信界面. 功能:展示四个可切换界面,当点击下方按钮时,界面随之切换. 布局:顶部和底部layout.主页面(中间放一个framelayout显示界面内 ...
最新文章
- 内网穿透工具 Ngrok
- mysql用户授权开发者_Mysql添加用户与授权
- Struts2中使用OGNL表达式语言访问静态方法和静态属性以及我遇到的问题和解决方法
- 数据分析数据可视化(三)
- asp.net 点击查询跳转到查询结果页面_【免费毕设】ASP.NET交通信息网上查询系统的设计与实现(源代码+论文+开题报告)...
- 域名被抢注了怎么办?
- 一元云购系统接入手机短信功能说明【V3版】
- Mac Pro 开不了机
- AD9854+STM32正弦波信号发生器
- 数组unshift方法及重构
- Linux服务部署-3构建nfs时间服务器
- PLC内部等效电路怎么设计?
- 10个流行常用的Django第三方包-大江狗推荐
- 解决ZooKeeper配置中出现Error contacting service. It is probably not running.
- Java 中验证时间格式的 4 种方法
- 传说很牛逼的BLT。
- 华为RIM设备短路故障
- 联发科智能音箱系统单芯片解决方案---MT8516芯片资料介绍
- 唐诗宋词学习·106~110节
- spotify使用教程_如何在Google Home中使用Spotify