アプリ化しよう
以前にKotlinアプリを作成したときにそれは不要だとか思っていたのですけど、よくよく考えてみたら僕のアイデンティティを保つのに必要なのではないかという疑問が浮かんだので作ることにしました(需要なし)。
そうしたわけで、今回も Android Studio (通称:あんスタ)で開発を行っていきたいと思います。まずは何はともあれ外装を整えていきます。レイアウトがHTML/CSSならレイアウトの書き方を逐一覚える必要なんてないんですけど、このレイアウト系はXMLなのでKotlinを覚えるついでに書き方もしぶしぶ覚えていかねばなりません。
というわけでレイアウトはこうなりました。
<?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:id="@+id/monBackground" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000" tools:context=".MainActivity"> <TextView android:id="@+id/monText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="テキスト" android:textColor="@android:color/holo_red_dark" android:textSize="30sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.965" /> <ImageView android:id="@+id/imageView" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:adjustViewBounds="false" android:scaleType="fitCenter" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:srcCompat="@drawable/medical_pest_ishi" /> </android.support.constraint.ConstraintLayout>
今回は画像も表示させたりなどしてみます(ImageView辺りの要素)。レイアウトに関してまじめにいじってないので機種によってかなり表示にブレがでる。今度いじるときはその辺を頑張ることにします。
背景の色や文字色はandroid:background
やandroid:textColor
をいじればいけるっぽい。これに関してまとまったドキュメント見つけられなかったから実際どんなものなのかわかんない。
そんなこんなでレイアウト(雑)ができたので、プログラムをいじります。
import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.graphics.Color import kotlinx.android.synthetic.main.activity_main.* import java.util.Calendar class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val cal = Calendar.getInstance() if (cal.get(Calendar.DAY_OF_WEEK) === 1) { monText.text = "フフフ…怖いか月曜だぞ" monText.setTextColor(Color.RED) monBackground.setBackgroundColor(Color.BLACK) imageView.setImageResource(R.drawable.medical_pest_ishi) } else { monText.text = "普通の日です" monText.setTextColor(Color.BLACK) monBackground.setBackgroundColor(Color.WHITE) imageView.setImageDrawable(null) } } }
毎度おなじみ、いつものクソ判定文。文字だけじゃ味気ないので画像のいじり方も調べながらやってみた。言語はKotlinでやってます。
これを実行するとこうなります。曜日で結果を振り分けてみる。


あー、これは神アプリですわ。間違いなく神アプリですわー(適当)。といった感じで、アプリがさっくりできるようなのでみんなも泥アプリつくっていこうな。
参考
Package Index | Android Developers
一応APIリファレンス載せておくけどここからほしい機能探すのつらいね。