Android Jetpack Compose基础之MaterialTheme主题样式
compose
compose - Docker Compose是一个用于定义和运行多容器Docker应用程序的工具,通过Compose文件格式简化应用部署过程。
项目地址:https://gitcode.com/gh_mirrors/compose/compose
免费下载资源
·
Android Jetpack Compose基础之MaterialTheme主题样式
在创建项目的时候,studio会默认生成一个以项目名+Theme名称的Theme函数,例如MyAPPTheme
MaterialTheme 使用
@Composable
fun MyAPPTheme(
darkTheme: Boolean = isSystemInDarkTheme(),
content: @Composable() () -> Unit
) {
val colors = if (darkTheme) {
DarkColorPalette
} else {
LightColorPalette
}
MaterialTheme(
colors = colors, //颜色集合
typography = Typography, //字体
shapes = Shapes, //形状
content = content //视图
)
}
private val DarkColorPalette = darkColors(
primary = Color_00c389,
primaryVariant = Purple700,
secondary = Teal200
)
private val LightColorPalette = lightColors(
primary = Color_00c389,
primaryVariant = Purple700,
secondary = Teal200
/* Other default colors to override
background = Color.White,
surface = Color.White,
onPrimary = Color.White,
onSecondary = Color.Black,
onBackground = Color.Black,
onSurface = Color.Black,
*/
)
看代码,其主要控制Colors颜色,接下来我们看下Color有哪些参数
MaterialTheme colors 配置
class Colors(
primary: Color, // 主颜色,屏幕和元素都用这个颜色
primaryVariant: Color, // 主颜色变种色,用于与主颜色做区分的场景,比如app bar和system bar
secondary: Color, // 次选色之强调色,悬浮按钮,单选/复选按钮,高亮选中的文本,链接和标题
secondaryVariant: Color, // 次选色变种色,用于区分强调色
background: Color, // 背景色,在可滚动项下面展示
surface: Color, // 平面色之表层色,展示在平面组件背景色,比如Surface组件,sheet组件和Menu菜单(CardView,SheetLayout,Menu)等
error: Color, // 错误色,组件中展示错误信息,比如TextField的提示信息
onPrimary: Color, // 在主颜色primary之上的文本和图标的颜色
onSecondary: Color, // 在强调色secondary之上的文本和图标的颜色
onBackground: Color, // 在背景色background之上的文本和图标的颜色
onSurface: Color, // 在表层色surface之上的文本和图标的颜色
onError: Color, // 在错误色error之上的文本和图标的颜色
isLight: Boolean // 是否是亮色模式
)
MaterialTheme Typography 配置
在Type.kt中定义app字体样式内容:
// Set of Material typography styles to start with
val Typography = Typography(
//一级内容文字样式
body1 = TextStyle(
fontFamily = FontFamily.Default,
fontWeight = FontWeight.Normal,
fontSize = 16.sp
)
/* Other default text styles to override
//按钮文字
button = TextStyle(
fontFamily = FontFamily.Default,
fontWeight = FontWeight.W500,
fontSize = 14.sp
),
//说明文字
caption = TextStyle(
fontFamily = FontFamily.Default,
fontWeight = FontWeight.Normal,
fontSize = 12.sp
)
,
//页脚页眉文字样式
overline = TextStyle(
fontFamily = FontFamily.Default,
fontWeight = FontWeight.Normal,
fontSize = 12.sp
)
//一级标题
h1 = TextStyle(
fontFamily = FontFamily.Default,
fontWeight = FontWeight.Normal,
fontSize = 12.sp
)
*/
)
MaterialTheme Shapes配置
在Shape.kt中定义app背景形状样式内容:
val Shapes = Shapes(
// 小组件使用的形状,比如: Button,SnackBar,悬浮按钮等
small = RoundedCornerShape(4.dp),
// 中组件使用的形状,比如Card(就是CardView),AlertDialog等
medium = RoundedCornerShape(4.dp),
// 大组件使用的形状,比如ModalDrawer或者ModalBottomSheetLayout(就是抽屉布局和清单布局)
large = RoundedCornerShape(0.dp)
)
MaterialTheme 原理
MaterialTheme它本身是一个Comsable组件,相关配置最终传入ProvideTextStyle,在其内部进行调用。
以Colors为例,其中使用CompositionLocalProvider函数通过provides提供给LocalColorScheme进行使用。
@Composable
fun MaterialTheme(
colorScheme: ColorScheme = MaterialTheme.colorScheme,
shapes: Shapes = MaterialTheme.shapes,
typography: Typography = MaterialTheme.typography,
content: @Composable () -> Unit
) {
val rippleIndication = androidx.compose.material.ripple.rememberRipple()
val selectionColors = rememberTextSelectionColors(colorScheme)
CompositionLocalProvider(
LocalColorScheme provides colorScheme,
LocalIndication provides rippleIndication,
androidx.compose.material.ripple.LocalRippleTheme provides MaterialRippleTheme,
LocalShapes provides shapes,
LocalTextSelectionColors provides selectionColors,
LocalTypography provides typography,
) {
ProvideTextStyle(value = typography.bodyLarge, content = content)
}
}
那我们是如何引用到MaterialTheme中的相关配置呢,如MaterialTheme.colorScheme.surface,接下来看单例MaterialTheme
object MaterialTheme {
/**
* Retrieves the current [ColorScheme] at the call site's position in the hierarchy.
*/
val colorScheme: ColorScheme
@Composable
@ReadOnlyComposable
get() = LocalColorScheme.current
/**
* Retrieves the current [Typography] at the call site's position in the hierarchy.
*/
val typography: Typography
@Composable
@ReadOnlyComposable
get() = LocalTypography.current
/**
* Retrieves the current [Shapes] at the call site's position in the hierarchy.
*/
val shapes: Shapes
@Composable
@ReadOnlyComposable
get() = LocalShapes.current
}
最终发现获取配色时,是通过MaterialTheme单例对象的colorScheme属性,间接使用了LocalColorScheme,而LocalColorScheme又是什么呢
/**
* CompositionLocal used to pass [ColorScheme] down the tree.
*
* Setting the value here is typically done as part of [MaterialTheme].
* To retrieve the current value of this CompositionLocal, use
* [MaterialTheme.colorScheme].
*/
internal val LocalColorScheme = staticCompositionLocalOf { lightColorScheme() }
由代码可知CompositionLocal,初始化值是lightColorScheme返回的Colors配置
GitHub 加速计划 / compose / compose
33.28 K
5.15 K
下载
compose - Docker Compose是一个用于定义和运行多容器Docker应用程序的工具,通过Compose文件格式简化应用部署过程。
最近提交(Master分支:3 个月前 )
5e3a0953
full diff: https://github.com/docker/cli/compare/v27.4.0-rc.1...8d1bacae3e49ed1d096eede8eef4ae851d7f2eae
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
7 天前
a2a3eb72
- full diff: https://github.com/docker/cli/compare/cb3048fbebb1...v27.4.0-rc.1
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
7 天前
更多推荐
已为社区贡献1条内容
所有评论(0)