在 RelativeLayout 中放兩個 TextView,設定 android:layout_alignParentRight="true" 和 android:gravity="right",但文字仍靠左顯示。
原始碼中:
<RelativeLayout
android:background="@drawable/roundedbutton" ... >
<TextView android:layout_alignParentRight="true" android:gravity="right" ... />
</RelativeLayout>
若 roundedbutton.9.png 九宮格標記錯誤,或 customrect 背景有非預期 Padding,會導致 RelativeLayout 實際內容區域偏移,使 alignParentRight 參考錯誤邊界。
android:background,觀察 TextView 是否正常靠右。padding、contentInsets、子 View 的 left/right 座標。<padding> 設定。<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="end"> <!-- 所有子 View 靠右 -->
<TextView ... />
<TextView ... />
</LinearLayout>
LinearLayout 的 gravity="end" 直接作用於所有子 View,不受背景 Drawable 內部結構影響,更穩健。
FrameLayout 疊加背景 View + 內容 View 分離。ConstraintLayout 可視覺化約束,避免 RelativeLayout 隱性錯誤。