android - 从代码填充 GridLayout 不会给出与 XML 相同的结果
问题描述
我正在尝试在代码中复制以下 XML:
<GridLayout
android:id="@+id/row2Grid"
android:layout_row="1"
android:layout_column="0"
android:layout_rowWeight="40"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:alignmentMode="alignMargins"
android:columnCount="3"
android:columnOrderPreserved="true"
android:padding="0px"
android:rowCount="2"
>
<!-- Box 1 -->
<androidx.cardview.widget.CardView
app:cardBackgroundColor="#00ff00"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="8"
android:layout_column="0"
android:layout_columnWeight="6"
android:layout_marginRight="3dp"
android:layout_marginBottom="3dp">
</androidx.cardview.widget.CardView>
<!-- Box 2 -->
<androidx.cardview.widget.CardView
app:cardBackgroundColor="#ff0000"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="8"
android:layout_column="1"
android:layout_columnWeight="6"
android:layout_marginRight="3dp"
android:layout_marginBottom="3dp"
app:cardElevation="8dp">
</androidx.cardview.widget.CardView>
</GridLayout>
上面看起来像这样,这就是我想要的:
首先,带有 id 的 GridLayoutrow2Grid
将保留为 XML。但是 Box 1 和 Box 2 不会,所以它们必须由代码生成并row2Grid
动态添加
为此,我创建了这个函数,它在 MainActivity 的 onCreate 函数中调用了两次:
@Override protected void onCreate(Bundle savedInstanceState) { ... generateBox("#ff0000"); generateBox("#00ff00"); ... }
public void generateBox(String colorStr) {
gridLayout = findViewById(R.id.row2Grid);
int row2children = gridLayout.getChildCount();
CardView cardView = new CardView(ctx);
cardView.setBackgroundColor(Color.parseColor(colorStr));
GridLayout.LayoutParams cardViewLayoutParams = new GridLayout.LayoutParams(GridLayout.spec(0), GridLayout.spec(row2children));
cardViewLayoutParams.columnSpec = GridLayout.spec(0,8.0f);
cardViewLayoutParams.rowSpec = GridLayout.spec(0,8.0f);
cardViewLayoutParams.height = 0;
cardViewLayoutParams.width= 0;
cardViewLayoutParams.setMargins(0,0, 12, 34);
gridLayout.addView(cardView, cardViewLayoutParams);
gridLayout.invalidate();
gridLayout.requestLayout();
}
但它不起作用,只会产生以下结果:
我究竟做错了什么?
解决方案
你知道当你为一个问题苦苦挣扎了几个小时,然后当你提出问题时,你就会为自己找到解决方案?
我一发布问题就看到了解决方案。这当然是由于我首先这样做的事实引起的:
GridLayout.LayoutParams cardViewLayoutParams = new GridLayout.LayoutParams(GridLayout.spec(0), GridLayout.spec(row2children));
cardViewLayoutParams.columnSpec = GridLayout.spec(0,8.0f);
cardViewLayoutParams.rowSpec = GridLayout.spec(0,8.0f);
当我创建时cardViewLayoutParams
,我在构造函数中指定 cardView 的行和列属性,但在其正下方覆盖它们。将其更改为以下内容:
GridLayout.LayoutParams cardViewLayoutParams = new GridLayout.LayoutParams();
cardViewLayoutParams.columnSpec = GridLayout.spec(row2children,8.0f);
cardViewLayoutParams.rowSpec = GridLayout.spec(0,8.0f);
使一切按预期工作
推荐阅读
- react-native - 如何在两个物理设备上运行 react-native 项目(expo)
- angular - 如何在一个内部捕获事件触发器
- ios - 如何缩放为 iPad 设计的 UIView 以与 iPhone 一起使用?
- .htaccess - htaccess - 将 url 从 /test 重写为 index.php?page=test
- python - nohup 命令仅适用于相对目录
- angular - .subscribe() 调用在按下浏览器的后退按钮时不会第二次执行
- javascript - 我可以基于非模板驱动的方式更改其他属性来影响 Angular 组件的属性吗?
- deployment - 使用 create-react-app 在应用程序上托管的 github 页面上未加载图像
- docker - PHPStorm中openresty + mobdebug + EmmyLua远程调试的工作配置请求
- c# - 如何使列表框半透明?