RICHMOND, Va. — Days before a planned gun rights rally that has set Virginia’s capital on edge, President Trump warned that state Democrats were threatening Americans’ right to bear arms.

“Your 2nd Amendment is under very serious attack in the Great Commonwealth of Virginia,” Mr. Trump wrote on Twitter on Friday. “That’s what happens when you vote for Democrats, they will take your guns away. Republicans will win Virginia in 2020. Thank you Dems!”

The state legislature, where Democrats recently took control for the first time in a generation, is considering several gun control bills, including measures that would require background checks for people purchasing firearms and give municipalities the authority to ban weapons in public buildings.

Thousands of people are expected to descend on the Capitol in Richmond on Monday to protest those measures, in a state where hunting is popular and gun ownership is common in rural areas. Several men with ties to right-wing militias who had planned to attend the rally have been arrested in recent days.