设计工具:
Android studio
Android开发者官网上下载 Android 开发者 | Android Developer
设计目的
设计一个微信界面Android界面
要求界面的样式和微信打开后的界面相似,并且底部的四个按钮可以点击跳转界面。界面上方栏有标题居中,界面中间显示内容,内容随下方栏的选择而切换,界面下方栏分成四个小板块可点击切换,且正在使用的界面的图标为绿色,没有使用的界面的图标为灰色。
布局设置1.top.xml
设置宽高字体大小,背景设为灰色,文字为黑色,并且文本内容居中
2.bottom.xml
对底端界面进行配置
进行布局的调整和图片资源的设置
在res文件夹的layout中新建bottom.xml,拖入TextView拖入ImageButton后会调用drawable,选取所需的图标
点击每个按钮后的内容设置
这里写四个fragment.xml对应四个按钮
以fragment_chat为例
其余三个只需稍作修改即可
activity_main.xmlactivity_main.xml里需要加上frameLayout和include
java文件MainActivity
package com.example.mywork;
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{
//Fragment
private Fragment fragment_first=new Fragment_chat();
private Fragment fragment_second=new Fragment_mail_list();
private Fragment fragment_third=new Fragment_find();
private Fragment fragment_fourth=new Fragment_me();
//底端菜单栏LinearLayout
private LinearLayout linear_first;
private LinearLayout linear_second;
private LinearLayout linear_third;
private LinearLayout linear_fourth;
//底端菜单栏中的Imageview
private ImageView imageView_first;
private ImageView imageView_second;
private ImageView imageView_third;
private ImageView imageView_fourth;
//底端菜单栏中的TextView
private TextView textView_first;
private TextView textView_second;
private TextView textView_third;
private TextView textView_fourth;
//FragmentManager
private FragmentManager fragmentManager;
@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.setImageResource(R.drawable.chat_green);
textView_first.setTextColor(getResources().getColor(R.color.colorViewPress));
}
@Override
public void onClick(View view) {
//每次点击之后,将所有的ImageView和TextView设置为未选中
restartButton();
switch(view.getId())
{
case R.id.chat:
//选择所点击的菜单对应的图层片段
selectFragment(0);
//将该菜单的点击状态置为点击态
imageView_first.setImageResource(R.drawable.chat_green);
textView_first.setTextColor(getResources().getColor(R.color.colorViewPress));
break;
case R.id.mail_list:
selectFragment(1);
imageView_second.setImageResource(R.drawable.mail_list_green);
textView_second.setTextColor(getResources().getColor(R.color.colorViewPress));
break;
case R.id.find:
selectFragment(2);
imageView_third.setImageResource(R.drawable.find_green);
textView_third.setTextColor(getResources().getColor(R.color.colorViewPress));
break;
case R.id.me:
selectFragment(3);
imageView_fourth.setImageResource(R.drawable.me_green);
textView_fourth.setTextColor(getResources().getColor(R.color.colorViewPress));
break;
default:
break;
}
}
//重置菜单的点击状态,设为未点击
private void restartButton() {
//设置为未点击状态
//第一片段
imageView_first.setImageResource(R.drawable.chat);
textView_first.setTextColor(getResources().getColor(R.color.black));
//第二片段
imageView_second.setImageResource(R.drawable.mail_list);
textView_second.setTextColor(getResources().getColor(R.color.black));
//第三片段
imageView_third.setImageResource(R.drawable.find);
textView_third.setTextColor(getResources().getColor(R.color.black));
//第四片段
imageView_fourth.setImageResource(R.drawable.me);
textView_fourth.setTextColor(getResources().getColor(R.color.black));
}
//初始化中间的部分的图层片段
private void initFragment(){
fragmentManager=getSupportFragmentManager();
FragmentTransaction transaction=fragmentManager.beginTransaction();
transaction.add(R.id.frame_chat,fragment_first);
transaction.add(R.id.frame_chat,fragment_second);
transaction.add(R.id.frame_chat,fragment_third);
transaction.add(R.id.frame_chat,fragment_fourth);
//提交事务
transaction.commit();
}
//初始化各底端的LinearLayout、ImageView和TextView组件
private void initView(){
linear_first=findViewById(R.id.chat);
linear_second=findViewById(R.id.mail_list);
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();
}
}
四个调用按钮的java文件
以Fragment_chat为例
package com.example.mywork;
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 super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.fragment_chat,container,false);
}
}
图片资源的设置
在res文件夹中的drawable中新建图片文件,以chat图标的两个文件为例
其中fillcolor设置颜色,颜色的代码可以自行百度选择喜欢的颜色,图片的代码为pathData的内容,这里的图片全部来自iconfont-阿里巴巴矢量图标库
可以自行在网站上找合适的图片进行使用
结果展示安装Android studio自带的模拟器尝试运行
可以看到简单的界面功能可以实现
代码已上传到gitee仓库墨黑色的白/Android作为初学者还有很多不足,希望大佬可以指正错误



