GridLayout崩溃应用程序

时间:2016-03-03 22:22:22

标签: android android-gridlayout

我有一个GridLayout,我想用java改变它。 我有以下代码:

GridLayout gridLayout = (GridLayout) findViewById(R.id.gridLayout1);
GridLayout.LayoutParams param =new GridLayout.LayoutParams();
param.rightMargin = 5;
param.topMargin = 5;
gridLayout.setLayoutParams(param);

为什么会崩溃?

2 个答案:

答案 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布局的任何内容