SALEM, Ore. — An Oregon high school student disciplined for wearing a T-shirt promoting a border wall reached a settlement of his lawsuit against the school district, his lawyers said Tuesday.

The case highlights the struggle among schools to balance free-speech rights with keeping students safe, as acrimony intensifies over the country’s immigration policies and enforcement.

Under the settlement announced Tuesday, the Hillsboro School District in Portland’s western suburbs must pay $25,000 for attorney fees and the principal had to write an apology.

“I brought this case to stand up for myself and other students who might be afraid to express their right-of-center views,” Addison Barnes, who was a senior at Liberty High School when the incident happened in January, said in a statement sent by his attorneys.

In January, Barnes wore a “Donald J. Trump Border Wall Construction Co.” T-shirt to a class where a discussion of immigration issues was scheduled. The shirt also said “The wall just got 10 feet taller,” a reference to a remark by President Donald Trump.

According to the lawsuit, a school official summoned Barnes and told him that at least one student and one teacher had been offended by the shirt. Barnes was told to either cover the shirt to go home.

Barnes went home, and his absence was listed as a suspension, which was later rescinded, the school district said.

One-third of Liberty’s student body is of Hispanic origin, some of their family members have been deported, there have been recent events such as a sit-in to protest immigration policy and there have been racially motivated incidents, the school district said in a statement Tuesday.

“Liberty High School administration believed they could reasonably forecast that Mr. Barnes’ shirt might cause other students to feel unsafe and could potentially lead to walkouts, altercations, or other disruptive actions,” the school district said. School administrators “acted out of an abundance of caution on behalf of the student body to ensure safety.”

Barnes said his First Amendment rights to free speech had been violated. A federal judge on May 29 issued a temporary restraining order, essentially barring the school from prohibiting Barnes from wearing the shirt, and said the school district had not justified its censorship.

The Benbrook Law Group P.C. of Sacramento, Calif., and Lynch Conger McLane LLP of Bend, Ore., which announced the settlement, noted that the high school had allowed one of its teachers to hang a sign promoting sanctuary cities that protect immigrants in the United States illegally.

Barnes said he was singled out for supporting Trump.

“Everyone knows that if a student wears an anti-Trump shirt to school, the teachers won’t think twice about it,” Barnes said Tuesday. “But when I wore a pro-Trump shirt, I got suspended. That’s not right.”

Mike McLane, partner with Lynch Conger McLane and a Republican member of the Oregon legislature, said: “Political speech, whether popular or not, is protected by the Constitution.”

The school district said Tuesday the incident represents a legal gray area. It cited cases in which federal courts have upheld school bans on speech that would be protected outside public schools, including T-shirts displaying the Confederate flag, the American flag and the phrase “We are not Criminals” worn by Hispanic students.

The school district said that instead of contesting the lawsuit, which would have been costly and disruptive, it instead compromised.

A letter from Principal Greg Timmons apologizes for the initial suspension charged to Barnes for leaving campus and any upset that may have caused, and wishes him well in the future, the district said.