android - 将视图添加到 flexboxlayout
问题描述
单击一个按钮,我以编程方式创建一个文本视图并将它们添加到 flexboxlayout。为什么只添加了一个视图?
mButton.setOnClickListener(new View.OnClickListener() {
int i = 1;
@Override
public void onClick(View v) {
flexboxLayout.setFlexDirection(FlexDirection.ROW_REVERSE);
flexboxLayout.setFlexWrap(FlexWrap.WRAP);
flexboxLayout.setJustifyContent(JustifyContent.CENTER);
final TextViewR textViewR = new TextViewR(getApplicationContext(), "Text " + Integer.toString(i++)); //setting text here
FlexboxLayout.LayoutParams lp = new FlexboxLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
lp.setOrder(1);
textViewR.setLayoutParams(lp);
flexboxLayout.addView(textViewR);
}
});
解决方案
我假设TextViewR
是你的习惯TextView
。
你应该改变
`FlexboxLayout.LayoutParams lp = new FlexboxLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);`
至
`FlexboxLayout.LayoutParams lp = new FlexboxLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);`
因为textViewR.setLayoutParams(lp);
将 flexboxlayout 参数设置为 textview 并且您的参数告诉 textview 匹配作为 flexbox 的父级。
推荐阅读
- elasticsearch - 选择弹性搜索索引中两个属性之间的平均时间
- c# - 如何在 c# core 2.1 中模拟 this.HttpContext.WebSockets.AcceptWebSocketAsync()
- eclipse - Eclipse:创建 Gradle 项目,但让 Eclipse “接受”应用程序文件的 Groovy
- javascript - 访问从父组件运行选择查询的属性时,使用异步管道可观察到的元素在元素中未定义/为空
- ansible - Vagrant box 没有在 WSL2 下设置运行
- java - 覆盖 Spring 启动健康端点
- pddl - 创建 PDDL 域和问题 - Game Kami
- laravel - 如何在 Laravel 上使用扩展重定向路由?
- powershell - Powershell 5.1 - 找不到类型 [MyType]:验证是否加载了包含此类型的程序集
- mysql - 使用 pgloader 从 Mysql 8 迁移到 postgres 失败