首页 > 解决方案 > 如何在约束布局周围创建边框以及标题选项

问题描述

我需要实现如下风格

在此处输入图像描述

但是,我有一个约束布局,里面有很多子布局,它类似于一个编辑文本视图,具有通过锁定编辑其他值来编辑特定值集的功能。

现在我知道如何使用下面的代码在约束布局周围给出边框

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@android:color/transparent" />
    <stroke
        android:width="1dp"
        android:color="#6B000000" />

    <corners android:radius="4dp" />
</shape>

但是,我不知道如何像上图那样设置标题。

我试图将带有负边距/填充的 textview 放在下面,但它没有用

<TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#00000000"
            android:fontFamily="sans-serif"
            android:text="@string/title"
            android:textColor="@color/logoDark"
            android:textCursorDrawable="@drawable/cursor_style"
            android:textSize="12sp"
            android:paddingTop="-20dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

标签: androidandroid-styles

解决方案


以上所有答案都集中在创建新布局上。但你不应该这样做。只需在约束布局下方创建一个 TextView 即可。

 <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="sans-serif"
        android:text="@string/time_range"
        android:textColor="@color/logoDark"
        android:textCursorDrawable="@drawable/cursor_style"

        // Below are very important to achieve title on border 
        android:paddingStart="2dp" 
        android:paddingEnd="2dp" 
        android:layout_marginStart="10dp" 
        android:background="@color/cnstrbkg" 
        app:layout_constraintBottom_toTopOf="@+id/cnstLyt" 
        app:layout_constraintStart_toStartOf="@+id/cnstLyt" 
        app:layout_constraintTop_toTopOf="@+id/cnstLyt" /> 

textview的定位是非常非常重要的事情。更改它以将文本视图移动到您想要的任何位置。


推荐阅读