首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

Android LinearLayout 添加標題欄:RelativeLayout 疊加實現

     編輯:程式設計綜合問答

需求

LinearLayout 設置邊框後,需在邊框上方顯示標題文字。

方案:RelativeLayout 包裹

<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 實現更靈活的約束關係。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved