Android布局中用@id替代@+id导致按钮布局异常问题排查
Hey there! Great job getting a grasp on @+id vs @id—you’ve got that exactly right:
@+id/your_idcreates a brand new ID that gets added to your project’s auto-generatedR.javafile@id/your_idis strictly for referencing an ID you’ve already defined somewhere else in your layout or resource files
That weird layout mess you saw after removing the + from your android:layout_toRightOf attribute? It’s almost definitely an Android Studio preview cache bug, not an issue with your actual code. Here’s the breakdown:
When you first used @+id/two in that reference, the IDE’s layout preview might have temporarily registered it as a new ID, even though you meant to point to an existing one. Once you switched to the correct @id/two, the preview engine didn’t immediately catch up—it was still holding onto that old cached state, which made your buttons look all out of whack.
Restarting Android Studio wipes that stale cache, so the preview can properly parse your layout and recognize that @id/two is a valid, existing reference. That’s why everything snapped back to normal after the restart.
If you hit this kind of preview hiccup again, you don’t need to restart the whole IDE—try these faster fixes first:
- Tap the Refresh button (the little circular arrow) in the top-right corner of the Layout Editor
- Run
Build > Clean Projectfollowed byBuild > Rebuild Projectto refresh yourR.javafile and layout resources - Flip back and forth between the Design and Code views a few times to force the preview to reload
内容的提问来源于stack exchange,提问作者Rocoder

