LinearLayout 設置邊框後,需在邊框上方顯示標題文字。
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:text="Group Title"
android:textAppearance="?attr/textAppearanceHeadline6"
android:background="@android:color/white"
android:paddingHorizontal="8dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/title"
android:orientation="vertical"
android:background="@drawable/border_shape">
<!-- 原有內容 -->
</LinearLayout>
</RelativeLayout>
TextView 設置 layout_alignParentTop 置頂,background="@android:color/white" 遮蓋邊框上邊線形成標題欄效果。LinearLayout 用 layout_below="@id/title" 定位在標題下方。ConstraintLayout 實現更靈活的約束關係。