IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> JetPack Compose UI 之HelloWord! 第一行代码 -> 正文阅读

[移动开发]JetPack Compose UI 之HelloWord! 第一行代码

一、简单介绍:
Compose UI 解释为声明式UI、目的就是要取代xml、解放双手。??
? 优缺点还是官网的吹嘘吧:https://developer.android.com/jetpack/compose
如果不理解声明式UI建议花五分钟视频了解下:
https://www.bilibili.com/video/BV1c5411K75r
?


前言:能被jetpack放到一个单独的lable里、可见Google对它的重视、它不在是某个单独的库、而是一套框架、或许未来将会终结xml的时代!

二、代码及预览
编译器预览图:

相关代码:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            NewsStore()
        }
    }

    @Composable
    fun NewsStore() {
        //MaterialTheme 可由可无、对布局基本没影响
        MaterialTheme {
            /**
             * Column 默认垂直排列
             * horizontalAlignment = Alignment.CenterHorizontally 水平局中
             * Modifier 包含了对布局对象的控制、大小 pading等信息
             */
            Column(
                horizontalAlignment = Alignment.CenterHorizontally,
                modifier = Modifier
                    .padding(20.dp)
                    .width(300.dp)
            ) {
//            图片呗
                /**
                 * painter 图片资源设置
                 * clip 图片圆角设置、分别对应topStart,topEnd,bottomEnd,bottomStart
                 * 其他属性省略
                 */
                Image(
                    painter = painterResource(id = R.drawable.header),
                    modifier = Modifier
                        .height(150.dp)
                        .width(200.dp)
                        .clip(RoundedCornerShape(20.dp, 20.dp, 10.dp, 10.dp)),
                    contentDescription = "这里是图片描述"
                )
                //类似 xml中space、用来做填充
                Spacer(modifier = Modifier.height(20.dp))
//            字体呗
                Text(text = "first", color = Color.Green)
                Text(text = "second", color = Color.Blue)
                Text(text = "three", color = Color.Magenta)
                Text(
                    text = "在本例中,文章的标题很短。但有时,一篇文章的标题很长,我们不希望过长的标题影响应用的外观。尝试更改第一个文本元素。",
                    color = Color.Cyan,
                    overflow = TextOverflow.Ellipsis,
                    maxLines = 2
                    )
            }
        }
    }

    /**
     * Preview 预览需要写上此注释
     */
    @Preview
    @Composable
    fun DefaultPreView() {
        NewsStore()
    }
}

?三:环境配置

?根目录build.gradle:

buildscript {
    ext.kotlin_version = "1.5.10"
    ext.compose_version = '1.0.0-rc02'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.0.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

?app 下build.gradle:

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'org.jetbrains.kotlin.android'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.qiqilego.myapplication"
        minSdkVersion 21
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildFeatures {           // 2
        compose true
    }

    composeOptions {        // 3
        kotlinCompilerVersion kotlin_version
        kotlinCompilerExtensionVersion compose_version
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {

    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.3.2'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

    implementation "androidx.compose.ui:ui:$compose_version"
    // Tooling support (Previews, etc.)
    implementation "androidx.compose.ui:ui-tooling:$compose_version"
    // Foundation (Border, Background, Box, Image, Scroll, shapes, animations, etc.)
    implementation "androidx.compose.foundation:foundation:$compose_version"
    // Material Design
    implementation "androidx.compose.material:material:$compose_version"
    // Material design icons
    implementation "androidx.compose.material:material-icons-core:$compose_version"
    implementation "androidx.compose.material:material-icons-extended:$compose_version"
    // Integration with observables
    implementation "androidx.compose.runtime:runtime-livedata:$compose_version"
    implementation "androidx.compose.runtime:runtime-rxjava2:$compose_version"

    // UI Tests
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"

    implementation "androidx.activity:activity-compose:$compose_version"

}

?注意:工程必须为Kotlin工程、

Kotlin版本:1.5.10及以上、

build:gradle:>=7.0.0

Android Studio 只能用预览版、Android Studio Bumblebee | 2021.1.1 Canary 6 不过和正式版不冲突。各忙各的

jdk版本必须?>=11?
jdk版本更新后的设置:

?jdk11 资源链接:https://download.csdn.net/download/BirdEatBug/20688997?spm=1001.2014.3001.5501

预览版android studio链接:https://download.csdn.net/download/BirdEatBug/20689040?spm=1001.2014.3001.5501

  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2021-08-04 11:19:40  更:2021-08-04 11:21:05 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/17 13:46:20-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码