原始代碼的問題
< view style = " display : flex; align-items : center; " > < text style = " line-height : 1; " > 全國</ text> < image src = " /images/xia.png" style = " height : 20rpx; width : 20rpx; display : block; " > </ image>
</ view>
問題1:<text>元素的默認行高影響對齊
<text>元素默認有行高(line-height),會導致文字實際占位高度大于字體大小 即使設置了align-items: center,圖片也會基于文字基線對齊,而不是視覺中心對齊
問題2:<image>元素的默認顯示方式
<image>默認是內聯(inline)元素,會受到文本基線對齊規則影響 圖片的垂直對齊方式默認為baseline,與文字基線對齊會導致微妙的偏移
修改后的代碼
< view style = " display : flex; align-items : center; " > < text style = " line-height : 1; " > 全國</ text> < image src = " /images/xia.png" style = " height : 20rpx; width : 20rpx; display : block; " > </ image>
</ view>
line-height: 1將行高設置為與字體大小相同 消除了文字上下多余的空白,使文字高度更精確 讓Flex的align-items: center能基于實際內容高度居中 將圖片從默認的inline改為block顯示 避免受到文本基線對齊的影響 使圖片完全遵循Flex容器的對齊規則