An online dynamic and optimized formation switching strategy is proposed for obstacle avoidance of multiple ground mobile robots in unknown obstacle environments. A formation knowledge base is built according to common formation shapes in formation control, and a formation obstacle-avoiding strategy including none formation switching, isomorphic formation switching and isomeric formation switching is deigned, in which environment constraints are taken into consideration fully. In the isomorphic formation switching mode, the formation can be contracted or expanded in size by changing the dilation factor while preserving the shape. In the isomeric formation switching mode, performance indices including formation distortion degree, energy consumption ratio and formation change convergence ratio are established. On basis of the indices, environment information detected by the leader robot and current formation shape, the optimal leader-follower topology structure is obtained by optimizing the proposed environment fitness function. Finally, simulation experiments in various environments are carried out to demonstrate that the proposed strategy is feasible and effective.