President Trump is losing the confidence of Latin America. And China is stepping up its game.

Trump has imposed tariffs on Latin American countries, demanded that Mexico pay for a border wall, put thousands of people at risk for deportation and proposed to cut American aid to the region.

At the same time, China sees an opportunity and is strengthening its ties.

This dynamic played out within hours on a single day.

On January 22, Chinese Foreign Minister Wang Yi spoke to dozens of Latin American leaders at a conference in Chile. He told them China and Latin America must work together to support free trade, "oppose protectionism" and "work for an open world economy."

That afternoon, Trump announced tariffs on washing machines and solar panels -- a move that will hurt Mexico, China and, to a smaller extent, Brazil.

"Literally and figuratively, the Trump administration is building walls against Latin America," says Kevin Gallagher, an expert on China-Latin America relations at Boston University. "The Chinese are proposing to build bridges, and they're actually doing it."

China has a history of unfair trade practices, and America has a track record of promoting free trade. So the role reversal has caught Latin American leaders by surprise.

Colombian President Juan Manuel Santos told CNN the scenario is like "the world upside down."

Related: NAFTA talks make progress but huge divide remains

Latin Americans lose confidence in Trump

The Trump administration proposed cutting aid to Latin America by 35% in its 2018 budget. It would have been the lowest amount since 2001, according to the Washington Office on Latin America, an advocacy group.

The cuts were mostly rejected by the Senate. But they would have hurt programs for education, disease prevention, combating drug trafficking and eradicating cocaine production.

"The message was 'you're on your own,'" says Adam Isaacson, a director at the Washington Office on Latin America. "This is a huge opportunity for China if it wants to increase its influence in Latin America."

The Trump administration also decided last April not to renew its contribution to a fund dedicated to development projects at the Inter-American Development Bank, a Washington institution created by George H.W. Bush that lends money to Latin American countries.

In October, Trump refused to increase overall U.S. contributions at the request of the World Bank, another major lender to Latin America. The administration criticized the World Bank for its lending to China.

Trump may have already lost influence. The share of Latin Americans who believe the U.S. president will do the right thing regarding world affairs has plummeted since he took office, according to a Pew survey.

Related: Trump slaps tariffs on washing machines and solar panels

It's not hard to find reasons.

Trump's insistence that Mexico pay for a border wall has drawn public rebuke from many leaders across the region. The administration has also threatened to withdraw from a trade deal with Mexico. It has put hundreds of thousands of legally protected Central Americans in the United States at risk of deportation. And it has slapped tariffs on exports from Brazil, Argentina and Colombia, among others.

Related: European Union heaps sanctions on Venezuelan leaders

The U.S. still holds sway. But ...

Experts note that the United States still has closer cultural and political ties with Latin America than China does. It also has greater economic influence with certain countries, such as Mexico and Colombia.

American businesses from McDonald's (MCD) to Chevron (CVX) have a major presence in the region, too, and some are working with governments on investment projects.

The Trump administration hasn't been absent. Vice President Mike Pence and other administration officials have visited. Secretary of State Rex Tillerson just went to Latin America. And Trump has imposed sanctions on Venezuela, a move supported by many other regional leaders.

In a speech last week, Tillerson outlined U.S. policy in Latin America: Hold Venezuela's government accountable, fight drug trafficking and provide aid that strives toward "mutually beneficial goals."

Tillerson warned Latin American countries not to engage with China, saying it provides "onerous loans" with few rewards. Tillerson said China's presence in the region is "alarming."

"Latin America does not need new imperial powers that seek only to benefit their own people," he said at the University of Texas.

But the next day, while Tillerson was meeting with Mexico's president, Trump was threatening to cut off aid to Latin American countries "if they can't stop drugs from coming in" to the United States. He specifically named Mexico, El Salvador, Guatemala and Honduras.

"These countries are not our friends, you know," the president said.

Related: Venezuela has lost half its economy in less than five years

China's footprint is growing fast

As American influence wanes, China wants to fill the gap. Chinese banks and institutions poured $23 billion into Latin America last year, the most since 2010, according to Derek Scissors at the American Enterprise Institute, who tracks China's investments around the world.

China's policy for the region is clear: Build, trade and build some more.

From bridges to ports to railways, China is trying to improve the region's connections to its own Pacific coast, where shipping containers are bound for the People's Republic.

In the past decade, China has become the top trading partner of Brazil, Peru, Chile and Argentina. It already imports plenty of soy, corn, iron ore and other raw commodities from the region.

"What we will see in the coming years is that China becomes the top trade partner of even more countries in the region," says Margaret Myers, a China-Latin America expert at the Inter-American Dialogue, a Washington research group.

"The approach that the Trump administration has used, the threatening approach ... what ends up happening is that the U.S. becomes a very useful foil for China," Myers adds.

Related: You can't get $1 out of the bank in Venezuela

China's experience in Latin America has been checkered, and in some instances a nightmare.

It tried to build a canal in Nicaragua. It was set to construct a high speed rail in Mexico. It dumped billions of dollars into Venezuela for trains and oil. All those projects failed, mostly because of local corruption, resistance or ineptitude. An attempt to build a transcontinental railway hasn't panned out.

But Chinese businesses and banks are undeterred. On top of the $23 billion that China poured into the region last year, the new, China-led Asia Infrastructure Investment Bank also lends to the region.

More broadly, Wang, China's foreign minister, told Latin American officials in late January that the region could play a "meaningful" role in China's One Belt One Road initiative, a collection of interlinking trade deals and infrastructure projects around the world.

But unlike Africa or Asia, China hasn't officially made Latin America part of its grand plan.

"China is not interested in provoking a U.S. response by including the entire Latin America and Caribbean region in its initiative," Myers says. "There are some concerns, certainly within the [Trump] administration, about China's presence in the region."