0
0

More than 3 years have passed since last update.

[Jetpack Compose] 今の時点でCompose利用する方法。

Posted at

対象

  • Composeを早く使いたい方

Jetpack

Jetpackのライブラリーが含めているもの。
image.png

中にComposeがみえますね。

Compose

composeは形状とデータの依存関係を記述するコンポーズ可能な関数を使用して、UI をプログラムで定義します。
先週ComposeはAlphaバージョンになりました。

Download

今はAndroid StudioのStableバージョンでは利用できません。
Canaryバージョンの設置が必要です。

image.png

  • Bin image.png

image.png

  • Studio64.exe

image.png

image.png

Composeが公式パージョンになると、後はStableチャンネルでも利用できると思います。

基本サンプルを作り方。

  • File -> New -> New Project

image.png

  • Empty Compose Activity

image.png

  • Nextをクリック。
  • あとはFinishをクリックします。

image.png

boilerplate Code

package com.dreamwalker.myapplication

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.Text
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.setContent
import androidx.ui.tooling.preview.Preview
import com.dreamwalker.myapplication.ui.MyApplicationTheme

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyApplicationTheme {
                // A surface container using the 'background' color from the theme
                Surface(color = MaterialTheme.colors.background) {
                    Greeting("Android")
                }
            }
        }
    }
}

@Composable
fun Greeting(name: String) {
    Text(text = "Hello $name!")
}

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    MyApplicationTheme {
        Greeting("Android")
    }
}

Preview機能。

image.png

image.png

image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0