Code example for GridView

Methods: setLayoutParamssetPaddingsetSelectorsetGravitysetHorizontalSpacingsetNumColumnssetStretchModesetVerticalSpacing, setOnItemClickListener

0
        super(context);
 
        mLayout = new LinearLayout(context);
        mLayout.setOrientation(LinearLayout.VERTICAL);
 
        gridView = new GridView(context);
        gridView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));// 选中的时候为透明色
        gridView.setNumColumns(4);
        gridView.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
        gridView.setVerticalSpacing(0);
        gridView.setHorizontalSpacing(0);
        gridView.setPadding(0, 0, 0, 0);
        gridView.setGravity(Gravity.CENTER);
        gridView.setOnItemClickListener(bodyClick);
        mLayout.addView(gridView);
 
        // 设置默认项 
        this.setContentView(mLayout);
        this.setWidth(LayoutParams.FILL_PARENT);
Experience pair programming with AI  Get Codota for Java