Androidソフトウェア開発
必要環境
・Android Studio (現状Ver4.0)
・Pleiades日本語化プラグイン (pleiades用だがAndroidStudioも日本語化可)
ヴァーチャル環境の生成
AVDマネージャから必要なスペック・androidのver.を選択し生成する
実機があるならUSBで接続してそちらを使用しても可
プロジェクトの生成
プロジェクトを生成するときに、対象にするandroidのver.が大事で
作るソフトの大きさやandroidのシェア率によって、どのver.までカバーするかを決定する
プロジェクト作成時の大事なファイル
・MainActivity.java : メインの動作を決定するjava
・AndroidManifest.xml : アプリ共通の設定ファイル
・activity_main.xml : アプリのレイアウトを決定するファイル
MainActivity
package local.ict.ict311.hello;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override //AppCompatActivityをオーバーライド
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//画面にR(res)/layout/activity_mainを表示させる
}
}
MainActivityで継承するActivityクラスはアプリの画面制御にかかわる基本的な機能を提供するクラスで、AppCompatActivity(アクションバーを利用したアプリ用)のほかにもたくさんの種類がある
上記のコードではres/layout/acivity_main.xmlで設定された画面レイアウトを画面に表示する処理を行っている
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- ↓画面全体(ConstraintLayout)の描写 -->
<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=".MainActivity">
<!-- ↓ウィジェット(部品:ここではTextView)をどこにどのように配置するかの設定 -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
<!-- ほんとは固定の文字列ではなく、Strings.xmlで設定した属性値で指定する -->
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.689"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.61" />
</android.support.constraint.ConstraintLayout>
実際はxmlを直接記述する必要はなく、activity_mainをデザインタブで開いて
ウィジェットをボタンから追加し、視覚的に配置してのレイアウトができる
テキストデータなどはStrings.xmlで設定した属性値を利用するのを推奨
・Strings.xmlの属性値を用いた描画
res/values/フォルダ下にあるStrings.xmlへ、nameに対して属性値を記述
することで、変数的にほかの複数ファイルに対して共通の属性値を与える
res/values/Strings.xml
<resources>
<string name="hello_world">こんにちは、世界!</string>
<!-- name:hello_worldに対して"こんにちは、世界!"という値を設定 -->
</resources>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/txtResult"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/hello_world" />
<!-- text="@string/hello_world"と記述することでアプリ上では
「こんにちは、世界!」と表示される -->
</LinearLayout>
またStrings.xmlをひらいている時に表示される「エディターを開く」ボタンを押下することで、多言語設定や視覚的な設定をすることもできる
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="local.ict.ict311.hello">
<!-- パッケージ名 -->
<application
android:allowBackup="true" <!-- バックアップの有無 -->
android:icon="@mipmap/ic_launcher" <!-- アプリの表示アイコン -->
android:label="@string/app_name" <!-- アプリの表示名 -->
android:roundIcon="@mipmap/ic_launcher_round" <!--表示アイコン(角丸)-->
android:supportsRtl="true" <!--アラビア語みたいな←読みの言語に対応するか-->
android:theme="@style/AppTheme"> <!--アプリのテーマ-->
<activity android:name=".MainActivity"> <!--アクティビティの設定-->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
使用されているActivityにそったアプリ全体の設定が記述されている