Android Kotlin Jetpack Compose 使用
Posted 安果移不动
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android Kotlin Jetpack Compose 使用相关的知识,希望对你有一定的参考价值。
效果如上。可以直接进行预览。
瞧瞧这类似Flutter的开发
package com.test.compose_demo
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.annotation.DrawableRes
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material.Icon
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.google.android.material.tabs.TabItem
import com.test.compose_demo.ui.theme.Compose_demoTheme
import com.test.compose_demo.ui.theme.black
import com.test.compose_demo.ui.theme.green3
import com.test.compose_demo.ui.theme.white1
class MainActivity : ComponentActivity()
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
setContent
Compose_demoTheme
WeBottomBar(1)
@Preview(showBackground = true)
@Composable
fun WeBottomBarPreview()
WeBottomBar(0)
@Preview
@Composable
fun TabItemPreView()
TabItem(
iconId = R.mipmap.ic_launcher1,
tint = MaterialTheme.colors.error,
title = "聊天",
modifier = Modifier
)
@Composable
private fun WeBottomBar(selected: Int)
Row(Modifier.background(white1))
TabItem(
iconId = if (selected == 0) R.mipmap.ic_launcher1 else R.mipmap.ic_launcher1,
tint = if (selected == 0) green3 else black,
title = "聊天", modifier = Modifier.weight(1f)
)
TabItem(
iconId = if (selected == 1) R.mipmap.ic_launcher else R.mipmap.ic_launcher,
tint = if (selected == 1) green3 else black,
title = "通讯录", modifier = Modifier.weight(1f)
)
TabItem(
iconId = if (selected == 2) R.mipmap.ic_launcher else R.mipmap.ic_launcher,
tint = if (selected == 2) green3 else black,
title = "发现", modifier = Modifier.weight(1f)
)
TabItem(
iconId = if (selected == 3) R.mipmap.ic_launcher else R.mipmap.ic_launcher,
tint = if (selected == 3) green3 else black,
title = "我", modifier = Modifier.weight(1f)
)
@Composable
private fun TabItem(
@DrawableRes iconId: Int,
title: String,
tint: Color,
modifier: Modifier = Modifier
)
Column(
modifier = modifier.padding(vertical = 8.dp),
horizontalAlignment = Alignment.CenterHorizontally
)
Icon(painterResource(id = iconId), title, Modifier.size(24.dp), tint = tint)
Text(title, fontSize = 11.sp, color = tint)
@Preview(showBackground = true)
标签可以生成预览
注解了两个方法
就预览了两个方法
以上是关于Android Kotlin Jetpack Compose 使用的主要内容,如果未能解决你的问题,请参考以下文章
开源100% Kotlin 的 Android Jetpack mvvm 项目!
Android kotlin 系列讲解(进阶篇)Jetpack系列之LiveData
Android Jetpack Kotlin/Java pageing3的基础使用。
Android Kotlin Jetpack Compose UI框架 完全解析