布局设计细节

  1. 要让 下划线View 的长度自动与 TextView 的值长度一致

要让 view_bottom_line 的长度自动与 tv_txt 的长度一致,你可以使用 ConstraintLayout 来替代 RelativeLayout,因为 ConstraintLayout 能更方便地实现视图之间的约束关系。以下是具体的实现步骤和代码示例:

效果图:

步骤分析

  1. 将 RelativeLayout 替换为 ConstraintLayoutConstraintLayout 可以通过约束条件精确控制视图的大小和位置。
  2. 设置 view_bottom_line 的左右边界与 tv_txt 一致:这样就能保证 view_bottom_line 的长度与 tv_txt 相同。

代码示例

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/rl_part"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:gravity="end|center"
    android:paddingLeft="10dp"
    android:paddingRight="10dp">

    <TextView
        android:id="@+id/tv_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="6dp"
        android:text="xxx标题"
        android:textColor="@color/selector_txt_757575_000_color"
        android:textSize="16dp"
        android:textStyle="bold"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

    <View
        android:id="@+id/view_bottom_line"
        android:layout_width="0dp" <!-- 使用 0dp 配合约束条件自适应宽度 -->
        android:layout_height="5dp"
        android:background="@drawable/bg_line"
        app:layout_constraintStart_toStartOf="@id/tv_text"
        app:layout_constraintEnd_toEndOf="@id/tv_text"
        app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

代码解释

  1. 使用 ConstraintLayout:将 RelativeLayout 替换为 androidx.constraintlayout.widget.ConstraintLayout,以便使用约束布局的功能。
  2. tv_text的约束设置
    • app:layout_constraintTop_toTopOf="parent" 和 app:layout_constraintBottom_toBottomOf="parent":使 TextView 在垂直方向上居中。
    • app:layout_constraintStart_toStartOf="parent" 和 app:layout_constraintEnd_toEndOf="parent":让 TextView 在水平方向上根据内容自适应宽度。
  3. view_bottom_line 的约束设置
    • android:layout_width="0dp":表示宽度根据约束条件自适应。
    • app:layout_constraintStart_toStartOf="@id/tv_text" 和 app:layout_constraintEnd_toEndOf="@id/tv_text":将 View 的左右边界与 TextView 的左右边界对齐,从而使它们的长度一致。
    • app:layout_constraintBottom_toBottomOf="parent":将 View 放置在父布局的底部。

通过以上设置,left_tab_deepseek_ai_bottom_line 的长度会自动与 tv_text的长度保持一致。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值