安卓仿微信界面惨烈版

该博客介绍了如何在Android应用中实现顶部和底部导航栏布局,并通过Java代码展示了如何管理四个Fragment,分别为微信、朋友、联系人和设置。点击不同导航栏项时,会显示对应的Fragment内容。

一,界面布局

1.top

<?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:layout_gravity="top"
    android:background="@color/black">

    <TextView
        android:id="@+id/textView6"
        android:layout_width="0dp"
        android:layout_height="65dp"
        android:layout_weight="1"
        android:gravity="center"
        android:textColor="@color/design_default_color_background"
        android:text="我的微信"
        android:textSize="30sp" />
</LinearLayout>

效果如图在这里插入图片描述

2.bottom

<?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:id="@+id/bottom"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:layout_gravity="bottom"
    android:baselineAligned="true"
    android:orientation="horizontal">

    <LinearLayout
        android:id="@+id/wechat"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical"
        tools:ignore="InvalidId">

        <ImageButton
            android:id="@+id/wechat_img"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:srcCompat="@android:drawable/sym_action_chat"
            tools:ignore="SpeakableTextPresentCheck" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="@string/wechat" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/friend"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical"
        tools:ignore="InvalidId">

        <ImageButton
            android:id="@+id/friend_img"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:srcCompat="@android:drawable/sym_action_chat"
            tools:ignore="SpeakableTextPresentCheck" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:gravity="center_horizontal"
            android:text="@string/friend" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/contact"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical"
        tools:ignore="InvalidId">

        <ImageButton
            android:id="@+id/contact_img"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:srcCompat="@android:drawable/sym_action_chat"
            tools:ignore="SpeakableTextPresentCheck" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="@string/contact" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/config"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical">

        <ImageButton
            android:id="@+id/config_img"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:srcCompat="@android:drawable/sym_action_chat"
            tools:ignore="SpeakableTextPresentCheck" />

        <TextView
            android:id="@+id/textView5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="@string/config" />
    </LinearLayout>
</LinearLayout>

效果如图

3.中间部分创建四个fragment

以其中一个wechat为例:

<?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"
    android:id="@+id/linearlayout_wechat"
    tools:context=".wechat">


    <TextView
        android:id="@+id/text_wechat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="这是微信聊天界面"
        android:textSize="50sp" />
</FrameLayout>

在这里插入图片描述

二,java文件

MainActivity

package com.example.mywork;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.NumberPicker;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

public class MainActivity extends AppCompatActivity  {
    private final Fragment wechat=new wechat();
    private final Fragment friend=new friend();
    private final Fragment contact=new contact();
    private final Fragment config=new config();
    private FragmentManager fm;
    private LinearLayout linearLayout1,linearLayout2,linearLayout3,linearLayout4;
    private TextView text_wechat,text_friend,text_contact,text_config;

    public MainActivity() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        linearLayout1 = findViewById(R.id.wechat);
        linearLayout2 = findViewById(R.id.friend);
        linearLayout3 = findViewById(R.id.contact);
        linearLayout4 = findViewById(R.id.config);
        linearLayout1.setOnClickListener(this::onClick);
        linearLayout2.setOnClickListener(this::onClick);
        linearLayout3.setOnClickListener(this::onClick);
        linearLayout4.setOnClickListener(this::onClick);
        initFragment();
        showfragment(0);

    }
    private void initFragment(){
        fm=getSupportFragmentManager();
        FragmentTransaction transaction=fm.beginTransaction();
        transaction.add(R.id.content,wechat);
        transaction.add(R.id.content,friend);
        transaction.add(R.id.content,contact);
        transaction.add(R.id.content,config);
        transaction.commit();
    }
    private void hideFragment(FragmentTransaction transaction){
        transaction.hide(wechat);
        transaction.hide(friend);
        transaction.hide(contact);
        transaction.hide(config);
    }
    public void onClick(View v){
        switch (v.getId()){
            case R.id.wechat:
                showfragment(0);
                break;
            case R.id.friend:
                showfragment(1);

                break;
            case R.id.contact:
                showfragment(2);

                break;
            case R.id.config:
                showfragment(3);

                break;
        }
    }
    private void showfragment(int i){//加图标颜色切换
        FragmentTransaction transaction= fm.beginTransaction();
        hideFragment(transaction);

        switch (i){
            case 0:
                transaction.show(wechat);
                break;
            case 1:
                transaction.show(friend);
                break;
            case 2:
                transaction.show(contact);
                break;
            case 3:
                transaction.show(config);
                break;
            default:
                break;
        }
        transaction.commit();
    }
}

wechat

package com.example.mywork;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

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

public class wechat extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_wechat, container, false);
    }

}

三,最终界面

在这里插入图片描述

四,其余完整代码

我的gitee仓库:完整代码

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值