Android分析HelloWorld

まず、Android-Manifest.xmlファイルを開きます。このファイルから、次のコードを見つけることができます。

<activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
</activity>

このコードは、HelloWorldActivityアクティビティの登録を表します。AndroidManifest.xmlに登録されていないアクティビティは使用できません。インテントフィルターの2行のコードは非常に重要です。actionandroid:name = "android.intent.action.MAIN" /であり、MainActivityがこのプロジェクトのメインアクティビティであることを示しています。電話のアプリケーションアイコンをクリックすると、アクティビティが最初に開始されます。
MainActivityを開くと、コードは次のようになります。

public class MainActivity extends AppCompatActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

MainActivityは、下位互換性のあるアクティビティであるAppCompatActivityから継承されます。これは、Android2.1システムのさまざまなシステムバージョンでActivityによって追加された機能と少なくとも互換性があります。アクティビティは、Androidシステムによって提供されるアクティビティ基本クラスです。プロジェクト内のすべてのアクティビティは、アクティビティ特性を持つために、プロジェクトまたはそのサブクラスから継承する必要があります(AppCompatActivityはアクティビティのサブクラスです)。次に、MainActivityにonCreate()メソッドがあることがわかります。これは、アクティビティの作成時に実行する必要があるメソッドです。コードは2行だけで、Hello World!はありません。言葉。onCreate()メソッドの2行目で、setContentView()メソッドが呼び出されます。このメソッドは、現在のアクティビティにactivity_main.xmlレイアウトを導入します。

<?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"
    tools:context=".MainActivity"><TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" /></androidx.constraintlayout.widget.ConstraintLayout>

コードにはTextViewがあります。
これは
、レイアウトにテキストを表示するためにAndroidシステムによって提供されるコントロールです。

おすすめ

転載: blog.csdn.net/weixin_42403632/article/details/115290627