Android Compose组件默认配置文档位置及组件默认样式列表查询
我完全懂你的挫败感!官方Material 3 Compose文档确实没把每个组件对应的默认主题样式(比如Typography、颜色)整理成统一列表,每次要找都得折腾——毕竟主题的核心意义就是让我们不用重复设置样式,结果连组件到底用了主题里的啥都得自己挖,这体验真的很差。
先给你几个实用的解决办法,不用再瞎试:
1. 直接查看Compose组件源码
Jetpack Compose是开源的,每个组件的默认参数都明明白白写在源码里,找起来其实挺直接:
- 比如查
OutlinedTextField的默认样式:打开它的源码,会看到textStyle参数默认值是LocalTextStyle.current,而在Material3主题的默认配置里,这个LocalTextStyle绑定的就是Typography.bodyMedium; - 它的
supportingText默认用的是Typography.bodySmall,还会根据错误状态自动切换颜色; - 标签文本
label的默认样式是Typography.labelLarge。
2. 用Compose DevTools快速定位
在Android Studio的Compose预览或者真机调试时,打开Compose DevTools(点击预览窗口的inspect按钮),选中你要查的组件,就能直接看到它的样式来源——包括用了主题里的哪个Typography样式、颜色值,不用翻源码就能快速搞定。
高频组件默认样式整理(基于Material3 Compose)
我根据源码整理了几个常用组件的默认配置,供你参考:
OutlinedTextField:输入文本→Typography.bodyMedium,支持文本→Typography.bodySmall,标签文本→Typography.labelLargeTextField(填充式):和OutlinedTextField一致,输入文本默认bodyMediumText:默认继承LocalTextStyle.current,无额外设置时对应主题的Typography.bodyMediumButton:按钮文本→Typography.labelLargeTopAppBar:标题文本→Typography.titleLarge,动作图标文本→Typography.labelLargeCard:内部文本默认继承父级LocalTextStyle,通常为bodyMedium
你此前的提问与GPT回复
I want a list of the defaults. for instance, what is the default fontstyle for the text inside a OutlinedTextField? It's meaningless to provide themes and their styles and colours unless I know what the components are using! I should not have to experiment to find out what text style OutlinedTextField is using. I want a list that says:
OutlinedTextField: bodyMedium for the text, bodySmall for the supportingText...
Text: bodyMedium for the text...
etc.
Finding a comprehensive list of default text styles for each specific Material Design 3 component in Jetpack Compose, like OutlinedTextField, is challenging. The official documentation does not explicitly list the default text styles for each component in a consolidated format. Instead, it provides information on how to use these components, their parameters, and customization options, including text styles.
确实如GPT所说,官方目前没有统一的汇总列表,希望上面的方法和整理内容能帮你少走点弯路。
备注:内容来源于stack exchange,提问作者cbn

