当前位置:   article > 正文

Android Studio搭建简单类微信小程序首页框架_android studio开发微信小程序

android studio开发微信小程序

目录

1、页面布局

2、代码具体实现

3、界面展示


1、页面布局

第一步:创建top.xml和bottom.xml.文件,用于设计界面顶部与底部按键。再修改activity_main.xml文件,将前面两个文件效果加入。最后设计四个功能界面。

1、 top.xml

用于首页标题显示

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="wrap_content"
  4. android:background="#4CAF50">
  5. <TextView
  6. android:id="@+id/textView"
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content"
  9. android:gravity="center"
  10. android:text="我的微信"
  11. android:textColor="@color/black"
  12. android:textSize="30sp" />
  13. </LinearLayout>

bottom.xml

四个linearlayout(线性布局)中均包含一个textview和一个imageview。

  1. <LinearLayout
  2. android:id="@+id/tab01"
  3. android:layout_width="0dp"
  4. android:layout_height="match_parent"
  5. android:layout_weight="1"
  6. android:gravity="center"
  7. android:orientation="vertical">
  8. <ImageView
  9. android:id="@+id/weixinButton"
  10. android:layout_width="match_parent"
  11. android:layout_height="49dp"
  12. app:srcCompat="@drawable/message" />
  13. <TextView
  14. android:id="@+id/weixinText"
  15. android:layout_width="match_parent"
  16. android:layout_height="wrap_content"
  17. android:gravity="center"
  18. android:text="微信"
  19. android:textColor="@color/black"
  20. android:textSize="20sp" />
  21. </LinearLayout>

2、修改activity_main.xml

导入顶部和底部布局,中间界面使用Framlayout

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:app="http://schemas.android.com/apk/res-auto"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical"
  7. tools:context=".MainActivity">
  8. <include
  9. layout="@layout/top"
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content" />
  12. <FrameLayout
  13. android:id="@+id/content"
  14. android:layout_width="match_parent"
  15. android:layout_height="wrap_content"
  16. android:layout_weight="1">
  17. </FrameLayout>
  18. <include
  19. layout="@layout/bottom"
  20. android:layout_width="match_parent"
  21. android:layout_height="wrap_content" />
  22. </LinearLayout>

3、四个fragment.xml文件编写

创建切换时的四个界面

这里以信息界面为例

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. tools:context=".WeChatFragment">
  6. <!-- TODO: Update blank fragment layout -->
  7. <TextView
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. android:gravity="center"
  11. android:text="这是微信消息界面"
  12. android:textSize="34sp" />
  13. </LinearLayout>

 2、代码具体实现

主要实现的功能:

1.点击时切换fragment内容
2.点击时改变底部被点击的按钮的颜色

每个fragment.xml文件对应一个fragment.java文件,更改页面id即可将页面内容放进类中。

  1. package com.example.wechat;
  2. import android.os.Bundle;
  3. import android.view.LayoutInflater;
  4. import android.view.View;
  5. import android.view.ViewGroup;
  6. import androidx.fragment.app.Fragment;
  7. public class WeChatFragment extends Fragment {
  8. @Override
  9. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  10. Bundle savedInstanceState) {
  11. // Inflate the layout for this fragment
  12. return inflater.inflate(R.layout.fragment_wechat, container, false);
  13. }
  14. }

 MainActivity.java文件编写

  1. package com.example.wechat;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import androidx.fragment.app.Fragment;
  4. import androidx.fragment.app.FragmentManager;
  5. import androidx.fragment.app.FragmentTransaction;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.ImageButton;
  9. import android.widget.ImageView;
  10. import android.widget.LinearLayout;
  11. import android.widget.TextView;
  12. public class MainActivity extends AppCompatActivity implements View.OnClickListener{
  13. private Fragment WeChatFragment;
  14. private Fragment friendFragment;
  15. private Fragment circleFragment;
  16. private Fragment configFragment;
  17. private FragmentManager fm;
  18. private ImageView weixinButton;
  19. private ImageView friendButton;
  20. private ImageView findButton;
  21. private ImageView SettingButton;
  22. private TextView weixinText;
  23. private TextView friendText;
  24. private TextView findText;
  25. private TextView SettingText;
  26. private LinearLayout tab01,tab02,tab03,tab04;
  27. @Override
  28. protected void onCreate(Bundle savedInstanceState) {
  29. super.onCreate(savedInstanceState);
  30. setContentView(R.layout.activity_main);
  31. WeChatFragment=new WeChatFragment();
  32. circleFragment=new circleFragment();
  33. friendFragment=new friendFragment();
  34. configFragment=new configFragment();
  35. tab01=findViewById(R.id.tab01);
  36. tab02=findViewById(R.id.tab02);
  37. tab03=findViewById(R.id.tab03);
  38. tab04=findViewById(R.id.tab04);
  39. weixinButton=findViewById(R.id.weixinButton);
  40. friendButton=findViewById(R.id.friendButton);
  41. findButton=findViewById(R.id.findButton);
  42. SettingButton=findViewById(R.id.SettingButton);
  43. weixinText=findViewById(R.id.weixinText);
  44. friendText=findViewById(R.id.friendText);
  45. findText=findViewById(R.id.findText);
  46. SettingText=findViewById(R.id.SettingText);
  47. fm=getSupportFragmentManager();
  48. initalfragment();
  49. tab01.setOnClickListener(this);
  50. tab02.setOnClickListener(this);
  51. tab03.setOnClickListener(this);
  52. tab04.setOnClickListener(this);
  53. }
  54. private void initalfragment() {
  55. FragmentTransaction transaction=fm.beginTransaction();
  56. transaction.add(R.id.content,WeChatFragment);
  57. transaction.add(R.id.content,friendFragment);
  58. transaction.add(R.id.content,circleFragment);
  59. transaction.add(R.id.content,configFragment);
  60. Hide(transaction);
  61. transaction.show(WeChatFragment);
  62. transaction.commit();
  63. }
  64. @Override
  65. public void onClick(View view) {
  66. resetBtn();
  67. switch (view.getId()){
  68. case R.id.tab01:
  69. show(1);
  70. break;
  71. case R.id.tab02:
  72. show(2);
  73. break;
  74. case R.id.tab03:
  75. show(3);
  76. break;
  77. case R.id.tab04:
  78. show(4);
  79. break;
  80. default:
  81. break;
  82. }
  83. }
  84. private void show(int i) {
  85. FragmentTransaction transaction=fm.beginTransaction();
  86. Hide(transaction);
  87. switch (i){
  88. case 1:transaction.show(WeChatFragment);weixinButton.setImageResource(R.drawable.message_pressed);break;
  89. case 2:transaction.show(friendFragment);friendButton.setImageResource(R.drawable.friend_pressed);break;
  90. case 3:transaction.show(circleFragment);findButton.setImageResource(R.drawable.circle_pressed);break;
  91. case 4:transaction.show(configFragment);SettingButton.setImageResource(R.drawable.setting_pressed);break;
  92. default:
  93. break;
  94. }
  95. transaction.commit();
  96. }
  97. private void Hide(FragmentTransaction transaction) {
  98. transaction.hide(WeChatFragment);
  99. transaction.hide(friendFragment);
  100. transaction.hide(circleFragment);
  101. transaction.hide(configFragment);
  102. }
  103. private void resetBtn(){
  104. weixinButton.setImageResource(R.drawable.message);
  105. friendButton.setImageResource(R.drawable.friend);
  106. findButton.setImageResource(R.drawable.circle);
  107. SettingButton.setImageResource(R.drawable.setting);
  108. }
  109. }

3、界面展示

      

                                    ​​​​​​​        ​​​​​​​

 

源码地址:

https://gitee.com/qian-jiahao/WeChat.git​​​​​​​

 

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/824561
推荐阅读
相关标签
  

闽ICP备14008679号