You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android开发:如何在卡片顶部边框居中添加文字

实现卡片顶部边框居中文本的方法

方法一:使用ConstraintLayout(推荐)

这是最灵活的实现方式,通过将TextView叠加在CardView顶部并偏移,实现文字覆盖边框的效果:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp">

    <!-- 基础卡片 -->
    <androidx.cardview.widget.CardView
        android:id="@+id/cardView"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        app:cardCornerRadius="8dp"
        app:cardElevation="4dp"
        app:cardUseCompatPadding="true"
        app:layout_constraintTop_toTopOf="parent">

        <!-- 卡片内部内容,自行替换 -->
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="卡片内部内容" />

    </androidx.cardview.widget.CardView>

    <!-- 顶部居中标题 -->
    <TextView
        android:id="@+id/cardTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="卡片标题"
        android:textSize="16sp"
        android:textStyle="bold"
        android:background="@color/white" <!-- 和卡片背景色一致 -->
        android:paddingHorizontal="12dp"
        android:paddingVertical="4dp"
        app:layout_constraintTop_toTopOf="@id/cardView"
        app:layout_constraintStart_toStartOf="@id/cardView"
        app:layout_constraintEnd_toEndOf="@id/cardView"
        android:layout_marginTop="-12dp" <!-- 向上偏移覆盖边框 -->
        app:layout_constraintHorizontal_bias="0.5" />

</androidx.constraintlayout.widget.ConstraintLayout>

方法二:使用RelativeLayout

如果项目还在使用RelativeLayout,也可以用类似思路实现:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp">

    <androidx.cardview.widget.CardView
        android:id="@+id/cardView"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        app:cardCornerRadius="8dp"
        app:cardElevation="4dp"
        app:cardUseCompatPadding="true">

        <!-- 卡片内部内容 -->
    </androidx.cardview.widget.CardView>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="卡片标题"
        android:textSize="16sp"
        android:textStyle="bold"
        android:background="@color/white"
        android:paddingHorizontal="12dp"
        android:paddingVertical="4dp"
        android:layout_centerHorizontal="true"
        android:layout_alignTop="@id/cardView"
        android:layout_marginTop="-12dp" />

</RelativeLayout>

关键注意事项

  • 确保TextView的背景色和CardView的背景色完全一致,避免出现视觉断层
  • 偏移量(layout_marginTop的负值)可以根据文字大小调整,一般设置为文字高度的一半左右
  • 如果卡片有圆角,可以给TextView的背景也设置相同的圆角,让视觉效果更统一
  • 若需要适配深色模式,建议使用主题色资源(如?android:colorBackground)代替固定颜色

内容的提问来源于stack exchange,提问作者Vuyyururajashekar Reddy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 13:20:32