我有一个GridLayout,我想用java改变它。 我有以下代码:
GridLayout gridLayout = (GridLayout) findViewById(R.id.gridLayout1);
GridLayout.LayoutParams param =new GridLayout.LayoutParams();
param.rightMargin = 5;
param.topMargin = 5;
gridLayout.setLayoutParams(param);
为什么会崩溃?
答案 0 :(得分:0)
LayoutParams
的类型由视图的父级决定。除非您的R.id.gridLayout1
位于其他GridLayout
内,否则您不应使用GridLayout.LayoutParams
。
答案 1 :(得分:0)
你可以这样做:
GridLayout gridLayout = (GridLayout) findViewById(R.id.gridLayout1);
PARENT_LAYOUT.LayoutParams layoutParams = ( PARENT_LAYOUT.LayoutParams) gridLayout.getLayoutParams();
layoutParams.rightMargin = 5;
layoutParams.topMargin = 5;
并将RankENT_LAYOUT替换为LinearLayout或您用作Root布局的任何内容