【发布时间】:2020-03-20 00:37:33
【问题描述】:
我正在尝试自定义材料TextInpuLayout.OutlinedBox 和TextInputEditText。
我现在的状态如下图
我想要做的是删除提示标签的背景,这样它就不会产生丑陋的切口。像这样:
或者,如果不可能将标签移到输入上方也可以解决问题:
如果可以使用样式来实现,那就太好了,这样我也可以轻松地将其应用于其他文本输入元素。 我对android还很陌生,所以请体谅。
这是样式的代码:
<style name="MyTheme" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<item name="android:textSize">14sp</item>
<item name="boxCornerRadiusTopStart">@dimen/textInputCornerRadius</item>
<item name="boxCornerRadiusTopEnd">@dimen/textInputCornerRadius</item>
<item name="boxCornerRadiusBottomStart">@dimen/textInputCornerRadius</item>
<item name="boxCornerRadiusBottomEnd">@dimen/textInputCornerRadius</item>
<item name="boxBackgroundColor">@color/primaryDarkColorBackground</item>
<item name="boxStrokeColor">@color/text_input_box_stroke</item>
</style>
<style name="EditTextStyle" parent="Widget.AppCompat.EditText">
<item name="android:background">@null</item>
<item name="android:paddingStart">30dp</item>
</style>
这是我在布局中定义输入的方式:
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/username_layout"
style="@style/MyTheme"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="48dp"
android:layout_marginEnd="48dp"
android:hint="@string/email_or_username"
app:boxBackgroundMode="outline"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.10">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/username"
style="@style/EditTextStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
【问题讨论】:
-
你能提供一些你目前已经实现的代码吗?
-
@Swayangjit 它只是默认的轮廓框,带有透明框描边、背景颜色和一点填充。
-
分享你的xml代码。
-
添加了一些xml代码进行说明。
-
我遇到了同样的问题。我正在构建一个 TextInputLayout 组件,我不知道它将被放置在哪个背景上,并且 HintText 背景与上面/OP 中的第一个示例具有相同的问题(文本周围的正方形)。也没有找到以编程方式定位它的方法。
标签: android android-textinputlayout material-components material-components-android android-textinputedittext