【问题标题】:Border with title in AndroidAndroid中带有标题的边框
【发布时间】:2020-07-05 18:23:08
【问题描述】:

我想在 Android 中创建一个组件。该组件应该是一个标题类似于TextInputLayout 的边框,并且可以在其中包含任何布局。

我在here 找到了一个解决方案,但我想知道是否有人可以分享一个不同的解决方案。我认为那个很hacky。

布局包括两个线性布局,第一个是带有标题的上边框线,第二个是带有左、右和下边框的内容。

你怎么看?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="20dp">
        <LinearLayout
            android:id="@+id/main"
            android:layout_width="match_parent"
            android:layout_height="20dp"
            android:orientation="horizontal"
            android:gravity="center_vertical"
            android:weightSum="2">
            <View
                android:layout_width="fill_parent"
                android:layout_height="2dp"
                android:layout_weight="1"
                android:background="#2ebadc"/>
            <TextView
                android:gravity="bottom"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20sp"
                android:text="Hello World" />
            <View
                android:layout_width="wrap_content"
                android:layout_height="2dp"
                android:layout_marginRight="5dp"
                android:background="#2ebadc"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_marginTop="10dp"
            android:padding="10dp"
            android:background="@drawable/topless_border">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="This is a border with title"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="The top inset of the border is set to -3 to make it disappear and then it's covered with a line with text in the middle by using LinearLayout with two Views and one TextView"/>
        </LinearLayout>
    </RelativeLayout>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:top="-3dp">
        <shape>
            <solid android:color="@android:color/transparent" />
            <stroke android:width="2dp" android:color="#2ebadc" />
            <corners android:radius="1dp" />
        </shape>
    </item>
</layer-list>

提前致谢。

【问题讨论】:

    标签: android user-interface kotlin material-design android-textinputlayout


    【解决方案1】:

    我研究了它是如何在 Material Components 库中实现的。他们为此使用CutoutDrawable。遗憾的是它不能直接使用,因为构造函数是包私有的。但是可以“直接”将这个类导入到项目中。

    为了获得预期的效果,我将向ConstraintLayout 添加两个视图,一个用于标签,一个用于内容。该解决方案还可以与任何其他父布局一起使用以定位视图,但使用 ConstraintLayout 可以轻松地根据需要定位视图:

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="200dp"
            android:layout_height="200dp">
    
            <View
                android:id="@+id/content"
                android:layout_margin="8dp"
                android:layout_height="0dp"
                android:layout_width="0dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
    
            <com.google.android.material.textview.MaterialTextView
                android:id="@+id/label"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:text="Test"
                app:layout_constraintBottom_toTopOf="@+id/content"
                app:layout_constraintLeft_toLeftOf="@+id/content"
                app:layout_constraintTop_toTopOf="@+id/content" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    

    内容视图将获得 CutoutDrawable。除了设置背景,我们还需要添加一些机制来更新位置被“切掉”——取决于标签视图的位置。我用OnLayoutChangeListener 解决了这个问题:

            // configuration of the shape for the outline
            val shape = ShapeAppearanceModel.Builder()
                .setAllCorners(RoundedCornerTreatment())
                .setAllCornerSizes(16f)
                .build()
    
            // configuration of the CutOutDrawable - replace with themed values instead of hard coded colors
            val drawable = CutoutDrawable(shape).apply {
                setStroke(4f, Color.BLACK)
                fillColor = ColorStateList.valueOf(Color.WHITE)
            }
            // content is the view with @+id/content
            content.background = drawable
            // label is the view with @+id/label
            label.addOnLayoutChangeListener { _, left, top, right, bottom, _, _, _, _ ->
                // offset the position by the margin of the content view
                val realLeft = left - content.left
                val realTop = top - content.top
                val realRigth = right - content.left
                val realBottom = bottom - content.top
                // update the cutout part of the drawable
                drawable.setCutout(
                    realLeft.toFloat(),
                    realTop.toFloat(),
                    realRigth.toFloat(),
                    realBottom.toFloat()
                )
            }
    

    还应该可以将此策略放入自定义视图中,该视图将CutoutDrawable 配置为背景,并为另一个视图的位置注册一个侦听器以更新剪切位置。

    这是结果:

    也可以通过调整标签TextView的padding来调整文字与行的间距

    【讨论】:

      猜你喜欢
      • 2018-03-24
      • 2015-12-17
      • 1970-01-01
      • 1970-01-01
      • 2015-04-09
      • 2019-12-30
      • 2018-01-13
      • 1970-01-01
      • 2021-08-19
      相关资源
      最近更新 更多