Compose - 其它设置、资源获取

二、对齐方式 Alignment

2.1 平面方向(2D)

TopStartTopCenterTopEnd
CenterStartCenterCenterEnd
BottomStartBottomCenterBottomEnd

2.2 线性方向(1D)

2.2.1 设置 verticalAlignment

Top
CenterVertically
Bottom

2.2.2 设置 horizontalAlignment

StartCenterHorizontallyEnd

三、排列方式 Arrangement 

Top垂直放置子元素,使他们尽可能的靠近顶部。效果:上123###下
Bottom垂直放置子元素,使他们尽可能的靠近底部。效果:上###123下
Start水平放置子元素,使他们尽可能的靠近起点。效果:左123###右(自动适配国际化)
End水平放置子元素,使他们尽可能的靠近终点。效果:左###123右(自动适配国际化)
Center子元素靠近中心挤着。效果:##123##(自动适配国际化)
SpaceBetween头尾子元素分布在两端,其余平均分布剩下空间。效果:1##2##3(自动适配国际化)
SpaceAround头尾子元素离两段占半个间隔,其余子元素分布剩下空间。效果:#1##2##3#(自动适配国际化)

3.1 设置 verticalArrangement

3.2 设置 horizontalArrangement

四、缩放方式 ContentScale

不像 ImageView 的 ScaleType 把缩放和对齐合在一起操作,Image 分成了2个不同的属性。

模式图片
宽高 > 屏幕宽高 < 屏幕宽 > 屏幕高 > 屏幕
不缩放

None

居中显示

裁剪超出部分

居中显示

四周留白

纵向:留白

横向:裁剪

纵向:裁剪

横向:留白

等比缩放

Inside

超出控件更长的边:匹配

超出控件较短的边:留白

居中显示

四周留白

纵向:留白

横向:匹配

纵向:匹配

横向:留白

Crop

裁剪

超出控件更长的边:裁剪

超出控件较短的边:匹配

超出控件更长的边:裁剪

超出控件较短的边:匹配

纵向:匹配

横向:裁剪

纵向:裁剪

横向:匹配

Fit

超出控件更长的边:匹配

超出控件较短的边:留白

超出控件更长的边:匹配

超出控件较短的边:留白

纵向:留白

横向:匹配

纵向:匹配

横向:留白

FillHeight

纵向:缩小到匹配

横向:剪裁或留白

纵向:拉伸到匹配

横向:裁剪或留白

纵向:匹配

横向:裁剪

纵向:匹配

横向:留白

FillWidth

纵向:剪裁或留白

横向:缩小到匹配

纵向:裁剪或留白

横向:拉伸到匹配

纵向:留白

横向:匹配

纵向:裁剪

横向:匹配

不等比缩放FillBounds宽高按各自比例拉缩小到匹配宽高按各自比例拉伸到匹配

纵向:拉伸后匹配

横向:缩小后匹配

纵向:缩小后匹配

横向:拉伸后匹配

下方图片:bigger = 图片宽高都比控件大,smaller=图片宽高都比屏幕小,taller = 图片高于控件高度,longger = 图片宽于控件宽度。参考文章

五、资源文件 Resource

避免使用定编码的方式,尺寸详见屏幕适配,颜色详见自定义主题

5.1 尺寸 dimens.xml

val smallPadding = dimensionResource(R.dimen.padding_small)
Text(
    text = "...",
    modifier = Modifier.padding(smallPadding)
)

5.2 颜色 colors.xml

//painterResource 可在主线程中解码并解析资源的内容。
Divider(color = colorResource(R.color.colorGrey))

5.3 字符串 strings.xml

Text(
    text = stringResource(R.string.compose)
)

//字符串复数
// <plurals name="runtime_format">
//    <item quantity="one">%1$d minute</item>
//    <item quantity="other">%1$d minutes</item>
// </plurals>
Text(
    text = pluralStringResource(R.plurals.runtime_format, quantity, quantity)
)

//字符串数组
//<string-array name="navLabels">
//    <item>主页</item>
//    <item>消息</item>
//    <item>个人</item>
//</string-array>
val navLabels = stringArrayResource(id = R.array.navLabels)

5.4 图片 drawable

Icon(
    painter = painterResource(id = R.drawable.ic_logo),
    contentDescription = null
)

六、TextStyle

参考文章

constructor(
    color: Color = Color.Unspecified,    //文字颜色
    fontSize: TextUnit = TextUnit.Unspecified,    //字体大小
    fontWeight: FontWeight? = null,    //字重
    fontStyle: FontStyle? = null,    //字体样式
    fontSynthesis: FontSynthesis? = null,
    fontFamily: FontFamily? = null,    //字体
    fontFeatureSettings: String? = null,
    letterSpacing: TextUnit = TextUnit.Unspecified,    //字间距
    baselineShift: BaselineShift? = null,
    textGeometricTransform: TextGeometricTransform? = null,
    localeList: LocaleList? = null,
    background: Color = Color.Unspecified,
    textDecoration: TextDecoration? = null,
    shadow: Shadow? = null,    //阴影
    textAlign: TextAlign? = null,    //对齐方式
    textDirection: TextDirection? = null,    //装饰(下划线、中划线)
    lineHeight: TextUnit = TextUnit.Unspecified,    //行高
    textIndent: TextIndent? = null
)

七、透明度 Alpha

取值范围 0f-1f,默认1f。

颜色滤镜 ColorFilter

在上层覆盖一层颜色(可用于选中后的颜色,适合单一颜色的图片,省去了几套图)。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值