android - Android XML 动态设置边距高度
问题描述
在 android 中,我需要让布局大小取决于 ViewModel 中的变量。例如:
<TextView
android:id="@+id/amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Example"
android:layout_marginTop="@{viewModel.interfaceEnable ? 0dp : 10dp}"
/>
根据 中的值viewModel.interfaceEnable
,它要么是 0dp,要么是 10dp。但我得到的错误是:
Syntax error: extraneous input 'p' expecting {'.', '::', '[', '+', '-', '*', '/', '%', '<<', '>>>', '>>', '<=', '>=', '>', '<', 'instanceof', '==', '!=', '&', '^', '|', '&&', '||', '?', ':', '??'}
如何通过这种检查设置 dp 或任何值?
解决方案
你可以这样做:
android:layout_marginTop="@{viewModel.interfaceEnable ? @dimen/no_margin : @dimen/margin_normal}"
推荐阅读
- r - 如何利用稀疏矩阵优化对多个移位点的评估
- php - 在 laravel 照片中重写 wordpress 网站后不显示
- java - 如何从不是使用“选择”方法/标签构建的下拉列表中选择一个选项?(硒Java)
- php - 有什么方法可以检查 Laravel 以前的 URL 中是否存在参数
- asp.net-core - 或 Asp.net Core 中授权属性中策略和角色之间的条件
- android - NoSuchMethodError:在 null 上调用了方法“>”
- .net-core - 外部输入 '-' 期望 {DECIMAL, '+', '-'}
- python - pytest 断言如果列在另一个已排序的组中是升序还是降序
- netsuite - NetSuite 修改装箱单 URL
- amazon-web-services - EKS Fargate - 用于没有出站互联网访问的私有集群的入口控制器?不支持 aws-alb-ingress