安卓开发之类微信的门户页面框架设计

设计目标

利用fragment,activity实现微信页面导航栏的功能,即能够通过点击导航栏实现页面的切换

功能说明

设置四个页面,通过switch-case进行页面的切换

代码解析

activity_main.xml文件,定义一个fragment

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    android:layout_gravity="center"
    tools:context=".MainActivity">

    <fragment
        android:id="@+id/fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="com.example.wechat.wechat_fragment"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="@+id/fragment"
        tools:ignore="MissingConstraints">


        <ImageView
            android:id="@+id/image1"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:src="@drawable/wechat"
            tools:ignore="MissingConstraints" />



        <ImageView
            android:id="@+id/image2"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:src="@drawable/message2"
            tools:ignore="MissingConstraints" />


        <ImageView
            android:id="@+id/image3"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:src="@drawable/discovery"
            tools:ignore="MissingConstraints" />

        <ImageView
            android:id="@+id/image4"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:src="@drawable/wode"
            tools:ignore="MissingConstraints" />
    </LinearLayout>

四个tab页面(图为发现页面代码,其他页面类似)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="发现"
        android:textColor="#009688"
        android:textSize="50sp" />
</RelativeLayout>

 main activity

public class MainActivity extends AppCompatActivity {


    private ViewPager mViewPager;
    private List<View> mViews;  //存放视图的数组
    private View view1,view2,view3,view4;
    private PagerAdapter mPagerAdapter;//适配器
    @SuppressLint("InflateParams")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageView imageView1=findViewById(R.id.image1);
        ImageView imageView2=findViewById(R.id.image2);
        ImageView imageView3=findViewById(R.id.image3);
        ImageView imageView4=findViewById(R.id.image4);
        imageView1.setOnClickListener(l);
        imageView2.setOnClickListener(l);
        imageView3.setOnClickListener(l);
        imageView4.setOnClickListener(l);

    }
    View.OnClickListener l=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FragmentManager fm;
            fm = getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            Fragment f;
            f = null;
            switch (v.getId()){
                case R.id.image1:
                    f=new wechat_fragment();
                    break;
                case R.id.image2:
                    f=new message_fragment();
                    break;
                case R.id.image3:
                    f=new find_fragment();
                    break;
                case R.id.image4:
                    f=new me_fragment();
                    break;
                default:break;
            }

            ft.replace(R.id.fragment,f);
            ft.commit();


        }
    };

运行展示截图

点击微信可以看到

 点击 通讯录可以看到

 

点击发现可以看到

点击我的页面

 

源码仓库地址

liuxinyi-monday/windows program

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值