首页 > 解决方案 > android中的Textview设计

问题描述

如何使我在 android 中的文本视图看起来像下图中的那样

在此处输入图像描述

标签: androidandroid-layoutmaterial-components-androidandroid-textinputlayoutmaterial-components

解决方案


用一个TextInputLayout

        <com.google.android.material.textfield.TextInputLayout
            app:boxBackgroundColor="@color/white"
            app:placeholderText="Name"
            app:hintEnabled="false">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

        </com.google.android.material.textfield.TextInputLayout>

在此处输入图像描述

否则,您可以将 aMaterialShapeDrawable应用于您的EditText(或TextView)。
在您的布局中定义:

        <EditText
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:id="@+id/edittext"
            android:paddingStart="4dp"
            android:hint="Name"/>

然后:

    val radius = resources.getDimension(R.dimen.cornerSize8)

    val textView: EditText = findViewById(R.id.edittext)
    val shapeAppearanceModel = ShapeAppearanceModel()
        .toBuilder()
        .setAllCorners(CornerFamily.ROUNDED, radius)
        .build()

    val shapeDrawable = MaterialShapeDrawable(shapeAppearanceModel)
    shapeDrawable.fillColor = AppCompatResources.getColorStateList(this,R.color.white)
    ViewCompat.setBackground(textView, shapeDrawable)

在此处输入图像描述


推荐阅读