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

类微信的门户页面框架设计

设计目标

根据课程教学内容完成类微信的门户页面框架设计,APP最少必须包含4个tab页面。框架设计需要使用fragment,activity,不得使用UNIAPP技术进行开发(H5或者小程序)

功能说明

在这里插入图片描述

门户页面分为top、content、bottom三个部分,通过点击bottom的四个tab可以实现content的转换,同时点击的tab变为深色

代码解析

top.xml

用textview展示主页面的标题–微信,设置内容居中

<?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/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="微信"
        android:gravity="center"
        android:textSize="30sp"/>
</LinearLayout>
bottom.xml

一个水平LinearLayout嵌套四个垂直LinearLayout,四个垂直LinearLayout比例为1:1:1:1,每个垂直LinearLayout包含显示图片的ImageView和显示文字的TextView,并设置clickable为true、gravity为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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="horizontal">
    
    <LinearLayout
        android:id="@+id/LinearLayoutWeixinFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="bottom"
        android:clickable="true"
        android:orientation="vertical">
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            app:srcCompat="@drawable/image_wechat" />
        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="微信"
            android:textSize="23sp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/LinearLayoutContactFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="bottom"
        android:clickable="true"
        android:orientation="vertical">
        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            app:srcCompat="@drawable/image_contact" />
        <TextView
            android:id="@+id/textView3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="联系人"
            android:textSize="23sp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/LinearLayoutFindFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="bottom"
        android:clickable="true"
        android:orientation="vertical">
        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            app:srcCompat="@drawable/image_find" />
        <TextView
            android:id="@+id/textView4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="发现"
            android:textSize="23sp"/>
    </LinearLayout>
    
    <LinearLayout
        android:id="@+id/LinearLayoutMineFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="bottom"
        android:clickable="true"
        android:orientation="vertical">
        <ImageView
            android:id="@+id/imageView4"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            app:srcCompat="@drawable/image_mine" />
        <TextView
            android:id="@+id/textView5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="我"
            android:textSize="23sp"/>
    </LinearLayout>
</LinearLayout>
fragment_wechat.xml(fragment_contact\fragment_find\fragment_mine与此类似)

设置TextView的layout_gravity和gravity均为center,layout_gravity是用来控制TextView在父控件FrameLayout中的位置

<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"
    tools:context=".WechatFragment">

   
    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="这是微信页面"
        android:textSize="30sp"/>

</FrameLayout>
activity_main.xml

通过include引入top和bottom,FrameLayout用来动态展示具体的Fragment

<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"
    tools:context=".MainActivity">
    <include layout="@layout/top"/>

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


    <include layout="@layout/bottom"/>
</androidx.constraintlayout.widget.ConstraintLayout>
WechatFragment类(ContactFragment\FindFragment\MineFragment与此类似)

定义一个继承Fragment的类WechatFragment,重写onCreateView方法,调用inflater.inflate加载fragment_wechat的布局文件

public class WechatFragment extends Fragment {
    public WechatFragment() {
        // Required empty public constructor
    }

    @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);
    }
}
MainActivity

在MainActivity主类中实现View.OnClickListener接口

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private View LinearLayout1,LinearLayout2,LinearLayout3,LinearLayout4;
    private FragmentManager fragmentManager;
    private ImageView image1,image2,image3,image4;
    private Fragment wechatFragment = new WechatFragment();
    private Fragment contactFragment = new ContactFragment();
    private Fragment findFragment = new FindFragment();
    private Fragment mineFragment = new MineFragment();

onCreate(),初始化四个LinearLayout并设置监听,初始化四个ImageView,调用initFragment(),showFragment()(参数为0,默认展示第一个tab的页面)

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        LinearLayout1 = findViewById(R.id.LinearLayoutWeixinFragment);
        LinearLayout2 = findViewById(R.id.LinearLayoutContactFragment);
        LinearLayout3 = findViewById(R.id.LinearLayoutFindFragment);
        LinearLayout4 = findViewById(R.id.LinearLayoutMineFragment);
        LinearLayout1.setOnClickListener(this);
        LinearLayout2.setOnClickListener(this);
        LinearLayout3.setOnClickListener(this);
        LinearLayout4.setOnClickListener(this);

        image1=findViewById(R.id.imageView1);
        image2=findViewById(R.id.imageView2);
        image3=findViewById(R.id.imageView3);
        image4=findViewById(R.id.imageView4);
        initFragment();
        showFragment(0);
    }

initFragment(),首先获取一个fragmentManager实例用来管理fragment,通过FragmentTransaction向Activity state中添加所有Fragment,最后调用commit()提交

    private void initFragment(){
        fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.content,wechatFragment)
                    .add(R.id.content,contactFragment)
                    .add(R.id.content,findFragment)
                    .add(R.id.content,mineFragment);
        transaction.commit();
    }

hideFragment()用于隐藏所有的Fragment

    public void hideFragment(FragmentTransaction transaction){
        transaction.hide(wechatFragment)
                    .hide(contactFragment)
                    .hide(findFragment)
                    .hide(mineFragment);
    }

showFragment(),通过FragmentManager获取FragmentTransaction,隐藏所有的fragment,根据传入参数i选择显示相应的fragment,并将对应的drawable中的image切换为深色的image

    private void showFragment(int i){
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        hideFragment(transaction);
        switch (i){
            case 0:
                transaction.show(wechatFragment);
                image1.setImageResource(R.drawable.image_wechat2);
                break;
            case 1:
                transaction.show(contactFragment);
                image2.setImageResource(R.drawable.image_contact2);
                break;
            case 2:
                transaction.show(findFragment);
                image3.setImageResource(R.drawable.image_find2);
                break;
            case 3:
                transaction.show(mineFragment);
                image4.setImageResource(R.drawable.image_mine2);
                break;
            default:
                break;
        }
        transaction.commit();
    }

resetImgs()用于将所有tab的图标置为初始状态白色,实质上是切换了一个image

    private void resetImgs(){
        image1.setImageResource(R.drawable.image_wechat);
        image2.setImageResource(R.drawable.image_contact);
        image3.setImageResource(R.drawable.image_find);
        image4.setImageResource(R.drawable.image_mine);
    }

onClick(),首先将所有tab的图标变换为白色,通过siwtch分支响应点击事件,展示对应的fragment

    @Override
    public void onClick(View v) {
        resetImgs();
        switch (v.getId()){
            case R.id.LinearLayoutWeixinFragment:
                showFragment(0);
                break;
            case R.id.LinearLayoutContactFragment:
                showFragment(1);
                break;
            case R.id.LinearLayoutFindFragment:
                showFragment(2);
                break;
            case R.id.LinearLayoutMineFragment:
                showFragment(3);
                break;
            default:
                break;
        }
    }
}

运行展示截图

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

源码仓库地址

https://gitee.com/xia-minzhe/AS-Work1.git

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 基于腾讯的小程序开发框架微信小程序具有很好的跨平台性,可以通过一套代码在微信、QQ、百度等多个平台上运行。 这是因为腾讯小程序开发框架采用了一种类似于 Web 开发的组件化开发方式,将页面拆分成多个组件,每个组件都有自己的逻辑和样式,并且可以在不同页面间复用。这种组件化的开发方式使得小程序具有很好的可维护性和可扩展性,同时也方便了跨平台开发。 在实际开发中,开发者只需要按照小程序开发规范编写代码,然后使用腾讯提供的小程序开发工具进行编译和调试即可。开发者不需要关心具体的平台差异,只需要在代码中使用小程序提供的 API 和组件即可。 需要注意的是,虽然小程序具有跨平台的能力,但是在不同的平台上仍然存在一些差异,例如 API 的支持情况、组件的样式表现等。因此,在进行跨平台开发时,开发者需要注意平台差异,并进行适当的兼容处理。 ### 回答2: 微信小程序具有很好的跨平台性要体现在以下几个方面。 首先,微信小程序基于腾讯的小程序开发框架,无需用户下载安装,只需在微信内打开即可使用,无论是在Android还是iOS设备上,都可以顺畅运行。这大大降低了用户使用的门槛,提供了更广泛的应用范围。 其次,微信小程序具有平台无关性,相同的代码可以同时适配多个平台。开发者只需开发一次应用,就可以在微信、QQ、百度、支付宝等多个平台上发布。这大大减少了开发成本,提高了开发效率。 再次,微信小程序具有良好的兼容性,无论是手机还是平板,无论是屏幕尺寸、分辨率还是操作系统版本,都能够适配。开发者无需为不同设备进行特殊的适配工作,可以更专注于功能和用户体验的设计。 此外,微信小程序提供了丰富的开发组件和接口,开发者可以利用这些组件和接口快速构建各种功能丰富的小程序应用。同时,微信小程序还提供了强大的调试工具和开发文档,开发者可以轻松进行调试和学习,进一步提升开发效率。 总的来说,基于腾讯的小程序开发框架微信小程序具有很好的跨平台性,通过降低使用门槛、提高开发效率以及优化用户体验,为用户提供了更广泛的应用范围和更好的使用体验。 ### 回答3: 微信小程序是由腾讯基于自身开发框架推出的一种应用程序,具有很好的跨平台性。首先,微信小程序能够在iOS和Android两种流操作系统上运行,覆盖了绝大部分移动设备用户。这使得开发者可以将应用程序直接发布到微信平台上,无需额外适配不同操作系统的版本,极大地简化了开发流程。 其次,基于腾讯的小程序开发框架微信小程序具有统一的开发语言和开发工具,即使开发者本身并不熟悉多个平台的开发语言,也能够快速上手进行开发开发者只需使用腾讯提供的开发工具包,使用一种统一的开发语言(通常为JavaScript),即可编写和调试代码。这样不仅降低了学习成本,也提高了开发效率。 此外,微信小程序还具有良好的用户体验。用户在微信中即可访问和使用小程序,无需下载和安装额外的应用程序。这大大降低了用户使用的门槛,同时也减少了手机存储空间的占用。另外,微信小程序提供了丰富的API和组件,方便开发者快速搭建功能丰富的小程序。 综上所述,基于腾讯的小程序开发框架微信小程序具有很好的跨平台性。它不仅能够在多个流操作系统上运行,还提供了统一的开发语言和工具,以及良好的用户体验。这使得开发者能够更加高效地开发和发布应用程序,同时为用户提供了更加便捷和丰富的使用体验。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值