In this study, dense anticorrosion magnesium–aluminum layered double hydroxide (MgAl-LDH) films were prepared for the first time by introducing a cationic surfactant tetradecyltrimethyl ammonium bromide (TTAB) in the process of in situ hydrothermal synthesis of Mg-Al LDH films on an AZ31 magnesium alloy. Results of XRD, FTIR, and SEM confirmed that TTAB forms the MgAl-LDH-TTAB, although TTAB cannot enter into LDH layers, and MgAl-LDH-TTAB powders are much smaller and more homogenous than MgAl-CO32−-LDH powders. Results of SEM, EDS, mapping, and XPS confirmed that TTAB forms the MgAl-LDH-TTAB films and endows LDH films with denser structure, which provides films with better shielding efficiency. Results of potentiodynamic polarization curves (PDP) and electrochemical impedance spectroscopy (EIS) confirmed that MgAl-LDH-TTABx g films have better corrosion resistance than an MgAl-CO32−-LDH film. The corrosion current density (icorr) of the MgAl-LDH-TTAB0.35 g film in 3.5 wt.% NaCl solution was reduced to 1.09 × 10−8 A.cm−2 and the |Z|f = 0.05 Hz value was increased to 4.48 × 105 Ω·cm2. Moreover, the increasing concentration of TTAB in MgAl-LDH-TTABx g (x = 0.025, 0.05, 0.1, 0.2 and 0.35) provided denser outer layer LDH films and thereby increased the corrosion resistance of the AZ31 Mg alloy. Additionally, the |Z|f = 0.05 Hz values of the MgAl-LDH-TTAB0.35 g film still remained at 105 Ω·cm2 after being immersed in 3.5 wt.% NaCl solution for 168 h, implying the good long-term corrosion resistance of MgAl-LDH-TTABx g films. Therefore, introducing cationic surfactant in the process of in situ hydrothermal synthesis can be seen as a novel approach to creating efficient anticorrosion LDH films for Mg alloys.