Android开发-类微信界面设计(一)

Android开发-类微信界面设计(一)

一、实现效果图如下:

image-20231014171856284

动画

二、功能说明:

设计一个类微信的主界面UI,有上中下三个结构,包含四个界面:聊天、联系人、位置、设置,并且实现点击下方进行界面跳转,并且在其中一个tab界面显示列表效果

三、知识点简单介绍

在Android开发中,XML文件Java文件是两种主要的文件类型,用于定义和控制应用程序的布局、界面和功能。

xml文件

XML文件用于定义应用程序的布局和界面。简单来说就是你能看到的效果图均是由它构成,相当于前端

java文件

Java文件是应用程序的逻辑和功能的实现。就是后端

四、实现说明

上中下三个结构

image-20231014174546111

观察可以很明显发现效果图布局为上中下,那么显示就需要在一个主xml(main.xml)上把三个xml显示出来(分别负责上中下)bottom.xmltop.xml中间显示的xml

由于中间有四个界面需要显示 所以再命名为tab1.xml,tab2.xml,tab3.xml,tab4.xml
main(主界面)

需要把三个部分的xml文件全部压入main.xml中

image-20231014184607181

用include包含上中下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <include layout="@layout/top" ></include>

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_gravity="center"
        android:layout_weight="1">

    </FrameLayout>

    <include layout="@layout/bottom"></include>


</LinearLayout>

top
效果

image-20231014180046346

很明显就一个位置在top文字

故在xml里添加一个textView组件就行

image-20231014180717335

代码:
<?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">



    <TextView
        android:id="@+id/textView"
        android:layout_width="815dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/black"
        android:shadowColor="@color/black"
        android:text="微信"
        android:gravity="center"
        android:textColor="@color/white"
        android:textSize="30sp" />  <!---->


</LinearLayout>
content(四个tab)
效果

image-20231014175643889

每个都类似,此处说明一个

tab2:

image-20231014181951597

代码
<?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/textView8"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:text="这是联系人界面"
        android:textSize="50sp" />

</LinearLayout>


bottom
bottom效果

image-20231014175932468

看出有4个图像 4个文字,一个图像和一个文字是一对的,4对横着排列

故布局如下:

image-20231014182614921

代码
<?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="wrap_content">

    <LinearLayout
        android:id="@+id/LinearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/imageView11"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:adjustViewBounds="true"

            app:srcCompat="@drawable/xiaoxi" />

        <TextView
            android:id="@+id/textView11"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:gravity="center"
            android:text="聊天"

            android:textSize="30sp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/LinearLayout2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/imageView22"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            app:srcCompat="@drawable/lianxiren" />

        <TextView
            android:id="@+id/textView22"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:shadowColor="@color/black"
            android:text="联系人"

            android:textSize="30sp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/LinearLayout3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"

        android:orientation="vertical">

        <ImageView
            android:id="@+id/imageView33"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            app:srcCompat="@drawable/shouye" />

        <TextView
            android:id="@+id/textView33"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:shadowColor="@color/black"
            android:text="位置"

            android:textSize="30sp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/LinearLayout4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/imageView44"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            app:srcCompat="@drawable/shezhi" />

        <TextView
            android:id="@+id/textView44"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:shadowColor="@color/black"
            android:gravity="center"
            android:text="设置"

            android:textSize="30sp" />
    </LinearLayout>
</LinearLayout>

注意!!

自定义的imag要放在drawable目录下并且在LinearLayout进行引用说明

image-20231014190744626

点击4个页面跳转功能

这个涉及到后端接口所以在java中实现

1.首先把java文件frament1.java与tab1.xml进行连接

(fragment作为中间桥梁达到main函数可以调用xml的效果)

故每个fragment都会连接到对应的tab

image-20231014184129928

package com.example.demo;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;


public class Fragment2 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        return inflater.inflate(R.layout.tab2, container, false);
    }
}
2.桥梁搭好之后在main函数功能实现具体的操作
(1)初始化

把4个fragment(tab页面)压入content里面

    private void initial() {//压入content
        FragmentTransaction ft=fm.beginTransaction()//事物 管理交互动作
                .add(R.id.content,fragment1)
                .add(R.id.content,fragment2)
                .add(R.id.content,fragment3)
                .add(R.id.content,fragment4);
        ft.commit();

    }
(2)显示当前页面
   private void fragmentshow(Fragment fragment) {
        FragmentTransaction ft=fm.beginTransaction()
                .show(fragment);
        ft.commit();
    }
(3)实现点击监听功能

注意!!

switch和if语句都可以实现

如果版本不允许的话(比如我)就可以用if语句实现

 switch(view.getId()){
            case R.id.LinearLayout1:fragmentshow(fragment1);break;
            case R.id.LinearLayout2:fragmentshow(fragment2);break;
            case R.id.LinearLayout3:fragmentshow(fragment3);break;
            case R.id.LinearLayout4:fragmentshow(fragment4);break;
      }
    public void onClick(View view) {
        fragmenthide();
        if (view.getId()==R.id.LinearLayout1){
            fragmentshow(fragment1);
        }else if (view.getId()==R.id.LinearLayout2){
            fragmentshow(fragment2);
        }else if (view.getId()==R.id.LinearLayout3){
            fragmentshow(fragment3);
        }else if(view.getId()==R.id.LinearLayout4){
            fragmentshow(fragment4);
        }

(4)实现页面隐藏功能
private void fragmenthide() {//藏起来
        FragmentTransaction ft=fm.beginTransaction()
                .hide(fragment1)
                .hide(fragment2)
                .hide(fragment3)
                .hide(fragment4);
        ft.commit();
    }
main函数操作

创建4个Fragment变量、1个管理对象FragmentManager变量 、4个LinearLayout变量对象

把LinearLayout实现与tab的图片文件进行初始化连接(get_id)

然后监听(setOnClicklistener)

然后进行全局初始化

把所有tab页面先藏起来

点到谁谁出来

image-20231014190312205

在tab中显示list功能

选择了tab1即聊天界面

需要用到RecycleView这个控件,在tab1.xml中加入,且创建一个item.xml(每一行的列表样式)

image-20231014191256892

也就是说你在tab1中加入的list样式,它的每一行其实是用item进行显示

然后创建Myadapter类连接item(与之前Fragment连接tab一样)

Fragment.java:

view=inflater.inflate(R.layout.tab1,container,false);
recyclerView=view.findViewById(R.id.recyclerview);//连接至tab1的控件
list=new ArrayList();
for(int i=0;i<9;i++){
    list.add("这是第"+i+"行数据");
}

adapter=new Myadapter(view.getContext(),list);//把list导入
LinearLayoutManager manager=new LinearLayoutManager(view.getContext());
manager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(manager);
// Inflate the layout for this fragment
return view;

主要作用是在Fragment的UI界面中展示一个列表视图(RecyclerView),并将数据源(List)提供给适配器(Adapter),以便进行数据的绑定和显示。

    LinearLayoutManager manager=new LinearLayoutManager(view.getContext());
  • 创建了一个LinearLayoutManager对象,传入了view.getContext()的上下文参数。通过这个参数,LinearLayoutManager可以获取到包含该RecyclerView的Activity或Fragment的Context对象,并在该上下文中设置LinearLayoutManager的属性。
manager.setOrientation(LinearLayoutManager.VERTICAL);
  • 对这个布局做界面调整 要垂直的
Myadapter.java说明

RecyclerView中的Myadapter

是一个管理数据和视图之间关系的类

为RecyclerView提供数据并创建对应的视图项

Adapter通常继承自RecyclerView.Adapter类,并通过重写其中的方法来实现自定义逻辑。例如,常见的方法包括:

  • onCreateViewHolder():用于创建ViewHolder对象,即每个Item对应的视图项。
 public Myholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view=LayoutInflater.from(context1).inflate(R.layout.item,parent,false);
        //布局的压缩
//        Inflater inflater=new Inflater();
//        inflater.inflate(); 真正的压缩
        Myholder myholder=new Myholder(view);

        return myholder;
    }
  • onBindViewHolder():用于将数据绑定到ViewHolder中的视图项上。

       public void onBindViewHolder(@NonNull Myholder holder, int position) {
    
            holder.textView.setText(list1.get(position));//用于设置TextView的文本内容为数据列表list1中指定位置的数据
           
            holder.textView.setTextColor(Color.RED); // 将颜色设置为红色
    
        }
    
    
  • getItemCount():返回数据集合的大小,即Item的数量。

    public int getItemCount() {
            return list1.size();
        }
    
  • 定义一个名为Myholder的自定义ViewHolder类,它继承自RecyclerView的内部类ViewHolder

​ 是用来表示每个数据项的视图项的,并且可以在需要时对其进行重用。ViewHolder通过保存视图项中的子视图 (例如TextView、ImageView等)的引用,以便在需要时快速地访问和更新视图内容

    public class Myholder extends RecyclerView.ViewHolder{
        TextView textView;

        public Myholder(@NonNull View itemView) {
            super(itemView);
            textView=itemView.findViewById(R.id.textView1);//引用item.xml里id为textView1的textView作为Myholder这个类中
            //textView对象的初始值
        }
    }

五、报错部分

1、error: ‘@tools:sample/avatars’ is incompatible with attribute src (attr) referencelcolor.

image-20231009100556565

用的是系统自带图片

自定义了2个src

solve:

把tools的那个删除

只留下

srcCompat:="@drawable/xiaoxi"

后面跟的是图片存放路径

2:图片命名飘红

image-20231009091749232

solve:

命名修改为英文的就行

3:没报错但打开程序一直闪退

一开始也不知道怎么办,上网搜索后才发现要查看日志

运行不报错,但是日志会报错(get了)

查找日志方法:

image-20231014194327863

4:Unable to instantiate activity ComponentInfo{com.example.demo/com.example.demo.MainActivity}:

说无法是实例化MainActivity

image-20231014194613213

查看后发现我创建的时候不小心加了一个abstract

image-20231014194647503

!!!:abstract是抽象类,抽象类是不行进行实例化的

5: Attempt to invoke virtual method ‘void android.widget.TextView.setText(java.lang.CharSequence)’

意思是在代码中尝试对一个空对象进行操作。具体来说,这个错误提示是因为在Myadapter.java文件的43行,尝试对一个TextView对象进行setText()方法的调用,但该TextView对象是null,即没有被正确地初始化或找到。

所以应该是getid的问题 没有正确的get到

image-20231014194929174

果然这里写错了

我的textView的id是textView1,但是我一开始写成立textView

而这个id是个空的

6、要滑动才行,不是老师要求的显示在一个界面

img

img

s:image-20231013100211283

把linearlayout中的height变为wrap_content

因为滑动显示是有关高度的参数

wrap_content 尽量缩小为合适的页面

match_parent 把父页面中找到显示合适的地方

六、Summary

第一次尝试Android开发,挺有趣但是也挺难,还是有很多不懂,希望后面自己能坚持总结写博客,解决完自己的errors之后和同学交流,其实也都是这些问题,解决完自己的问题之后,感觉收获很大,印象很深

希望总结下来,减少大家浪费的时间

源码地址:https://gitee.com/jardny/My_AndroidWork

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Android开发中,集成微信和QQ登录通常涉及到使用第三方提供的SDK,因为这两个平台提供了官方的开放接口供开发者接入。以下是集成微信和QQ登录的基本步骤: 1. **注册账号**:首先,你需要在微信开放平台(https://open.weixin.qq.com)和QQ开放平台(https://open.qq.com)上创建开发者账户,并申请相应的AppID。 2. **下载SDK**:从各自的官网下载微信Android SDK(WeChatForAndroid)和QQ登录SDK。 3. **添加依赖**:将SDK添加到你的项目中,通常是通过Gradle库管理或者将jar文件添加到项目的libs目录。 4. **配置权限**:在AndroidManifest.xml中添加必要的权限,如`<uses-permission android:name="android.permission.INTERNET" />`。 5. **初始化SDK**:在应用启动时,初始化微信和QQ的登录模块,设置回调和应用信息。 6. **调用授权登录**:当用户需要登录时,调用SDK提供的`login()`或`authorize()`方法,会跳转到相应平台的授权页面。 7. **处理回调**:在接收到用户的登录结果后,通过回调处理机制获取code、access_token等信息。 8. **交换token**:使用获取到的code换取长期有效的token,用于后续的API调用。 9. **保存用户信息**:将登录状态和用户信息存储在本地,比如SharedPreferences或数据库。 **相关问题--:** 1. 需要哪些Android权限来集成微信和QQ登录? 2. 在授权成功后,如何将获取到的用户信息同步到本地? 3. 如何处理微信和QQ登录过程中可能出现的错误或异常情况?

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值