初探安卓module

前言

最近趁着年假,想学习一下组件化,本人菜鸟一枚哈,本文主要也是写给我这样的小白,希望有所帮助,大神请多多批评指正

1.创建app工程

打开android studio–>file–>new project,一直next,最后finish

2.添加Button

我们在activity中添加一个button用于按下后跳转到module的activity
activity_main.xml文件添加:

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/moduleButton"
        android:text="MODULE"
        />

MainActivity.java文件:

public class MainActivity extends AppCompatActivity {

    private Button moduleButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        moduleButton = findViewById(R.id.moduleButton);


        moduleButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent moduleIntent = new Intent(getApplicationContext(), moduleActivity.class);
                startActivity(moduleIntent);
            }
        });
    }

}

3.新建module

file–>new module–>Phone & Tablet Module–>next,
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

4.修改module的activity界面

moduleTest模块的activity_module.xml文件:

<TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="module Test!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

5.修改module的AndroidMainfest.xml文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.moduletest">

    <application
            android:theme="@style/AppTheme">
        <activity android:name=".moduleActivity" />
    </application>

</manifest>

:1. 去掉application中的

			android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
	     2.去掉activity中的<intent-filter>内的
	     ```
	      <action android:name="android.intent.action.MAIN"/>

          <category android:name="android.intent.category.LAUNCHER"/>
            ```

5.修改module的build.gradle文件

修改apply plugin: 'com.android.application'apply plugin: 'com.android.library'
去掉applicationId "com.example.moduletest"
注意:module的build.gradle中的compileSdkVersion要与app的build.gradle中的compileSdkVersion保持一致

6.修改app的build.gradle文件

在dependencies{}中添加module依赖implementation project(path: ':moduletest')

7.录屏效果

在这里插入图片描述

总结:

关键在于build.gradle配置和AndroidMainfest.xml文件

发布了5 篇原创文章 · 获赞 2 · 访问量 1314

猜你喜欢

转载自blog.csdn.net/u011532067/article/details/86763748