簡體   English   中英

TextInputLayout 移動提示標簽並在聚焦時更改其背景顏色

[英]TextInputLayout move the hint label and change it's background color when focused

我正在嘗試自定義材料TextInpuLayout.OutlinedBoxTextInputEditText

我目前的狀態如下圖

在此處輸入圖片說明

我想要做的是刪除提示標簽的背景,以便它不會創建丑陋的切口。 像這樣:

在此處輸入圖片說明

或者,如果不可能將標簽移動到輸入上方也可以解決問題:

在此處輸入圖片說明

如果使用樣式可以實現這一點就好了,這樣我也可以輕松地將其應用於其他文本輸入元素。 我對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>

我認為您無法通過TextInputLayout.OutlinedBox樣式獲得它。
這不完全是您要找的:

   <com.google.android.material.textfield.TextInputLayout
        style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.Rounded"
        ..>

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

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

和:

  <style name="Widget.MaterialComponents.TextInputLayout.FilledBox.Rounded" parent="Widget.MaterialComponents.TextInputLayout.FilledBox">
    <item name="shapeAppearanceOverlay">@style/Rounded_ShapeAppearanceOverlay.MaterialComponents.TextInputLayout</item>
    <item name="boxStrokeColor">@color/input_text_no_underline</item>
  </style>

  <style name="Rounded_ShapeAppearanceOverlay.MaterialComponents.TextInputLayout" parent="">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">50%</item>
  </style>

選擇器用於去除下划線:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:alpha="0" android:color="?attr/colorOnSurface" android:state_hovered="true"/>
  <item android:alpha="0" android:color="?attr/colorOnSurface" android:state_enabled="false"/>
  <item android:alpha="0" android:color="?attr/colorOnSurface"/>
</selector>

在此處輸入圖片說明 在此處輸入圖片說明

注意:它需要庫的 1.1.0 版本。

嘗試在TextInputEditText上設置:

android:background="@android:color/transparent"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM