SACRAMENTO — California on Monday became the first state to enshrine certain rights for transgender K-12 students in state law, requiring public schools to allow those students access to whichever restroom and locker room they want.

Democratic Gov. Jerry Brown announced that he had signed AB1266, which also will allow transgender students to choose whether they want to play boys’ or girls’ sports. The new law gives students the right “to participate in sex-segregated programs, activities and facilities” based on their self-perception and regardless of their birth gender.

Supporters said it will help reduce bullying and discrimination against transgender students. It comes as the families of transgender students have been waging local battles with school districts across the country over what restrooms and locker rooms their children can use, disagreements that have sometimes landed in court.

The National Center for Lesbian Rights and the ACLU of California were among the bill’s supporters. Detractors, including some Republican lawmakers, said allowing students of one gender to use facilities intended for the other could invade the other students’ privacy.

Such fears are overblown, said Carlos Alcala, spokesman for the bill’s author, Democratic Assemblyman Tom Ammiano of San Francisco. In general, he said, transgender students are trying to blend in and are not trying to call attention to themselves.

“They’re not interested in going into bathrooms and flaunting their physiology,” Alcala said.

He also noted that the state’s largest school district, Los Angeles Unified, has had such a policy for nearly a decade and reported no problems. San Francisco schools also have such policies, and numerous other districts signed on in support of the legislation.

“Clearly, there are some parents who are not going to like it,” Alcala said. “We are hopeful school districts will work with them so no students are put in an uncomfortable position.”

The Gay-Straight Alliance Network said two states, Massachusetts and Connecticut, have statewide policies granting the same protections, but California is the first to put them into statute and require them in all school districts.