カテゴリー:
Android Studio ではじめる Android プログラミング入門 第3版 Android Studio 2対応
閲覧数:332 配信日:2018-03-09 13:46
Androidアプリの最もシンプルなソースコード
Activityクラス
・Androidアプリの最も基本となるクラス
MainActivityクラス
・android.support.v7.appパッケージの「AppCompatActivity」クラスを継承して作成されている
onCreateメソッド
・Bundleクラスのインスタンスが引数として渡される
・スーパークラスのonCreateメソッド呼出
・レイアウト表示
Bundleクラス
・アプリ情報を保管するための役割を果たすクラス
R.layout.activity_main
・リソースの「layout」フォルダにある「activity_main.xml」を示す定数
・この定数を参照する値が「R.layout.activity_main」となる
▼L:\Android\AndroidStudioProject\App20180302\app\src\main\res\layout\activity_main.xml
MainActivity.java
▼L:\Android\AndroidStudioProject\App20180302\app\src\main\java\tokyo\w3c\android0\app20180302\MainActivity.java
package tokyo.w3c.android0.app20180302;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
▼L:\Android\AndroidStudioProject\App20180302\app\src\main\res\layout\activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="tokyo.w3c.android0.app20180302.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" />
</android.support.constraint.ConstraintLayout>
xmlns:☆☆属性
・XML名前空間
・AndroidにおけるXMLの要素を利用するためのもの
・これらを記述することで、ここに書かれているタグがAndroidのレイアウト用のものとして認識されるようになる
tools:☆☆属性
・デザインツールでの挙動に関する設定
※アプリが実際に表示される際の設定ではない
・デフォルト設定値から変更する必要はない