【问题标题】:TextInputLayout passwordToggle with rounded cornersTextInputLayout 密码圆角切换
【发布时间】:2017-07-26 02:29:04
【问题描述】:

我正在使用来自 android 设计库版本 25.1.1 的 TextInputLayout。使用以下代码:

<android.support.design.widget.TextInputLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:local="http://schemas.android.com/apk/res-auto"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  local:passwordToggleEnabled="true"
  local:hintEnabled="false">
  <android.support.design.widget.TextInputEditText
    android:id="@+id/confirmationEditText"
    android:singleLine="true" />
</android.support.design.widget.TextInputLayout>

但是当按下密码切换图标时,它的涟漪效果会在 TextInput 的背景之上绘制:

如何为passwordToggle 设置圆角半径?我可以引用它现有的背景并用所需的属性“包装”它(如何找到切换使用的默认可绘制对象的路径)?

【问题讨论】:

    标签: android android-layout xamarin android-design-library android-textinputlayout


    【解决方案1】:

    为此使用自定义形状:

      <shape xmlns:android="http://schemas.android.com/apk/res/android">
             <gradient
                android:endColor="@color/something"
                android:centerColor="@color/something_else"
                android:startColor="@color/something_else_still"
                android:angle="270" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    

    【讨论】:

      【解决方案2】:

      我尝试在新项目上实施以了解您的方案。

      请查看解决方案。我附上了它的外观截图。

      您必须在drawable文件夹中包含drawable并将其设置为TextInputEditText

      的背景

      round_corner_toggle.xml

      <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
          <item android:left="20dp">
              <shape android:shape="rectangle" >
                  <size android:height="20dp" />
                  <solid android:color="#d8d8d8" />
                  <corners android:radius="5dp" />
              </shape>
          </item>
      
          <item android:right="60dp">
              <shape android:shape="rectangle" >
                  <size android:height="20dp" />
                  <solid android:color="#ecf0f1" />
                  <corners android:radius="5dp" />
      
              </shape>
          </item>
      </layer-list>
      

      TextInputLayout 的内容

                  <android.support.design.widget.TextInputLayout
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      app:counterEnabled="true"
                      app:counterMaxLength="8"
                      android:background="#FFFFFF"
                      app:passwordToggleEnabled="true"
                      app:passwordToggleTint="@color/colorPrimary">
      
                      <android.support.design.widget.TextInputEditText
                          android:id="@+id/tietPassword"
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:hint="Password"
                          android:background="@drawable/round_corner_toggle"
                          android:inputType="textPassword"
                          android:padding="@dimen/activity_horizontal_margin"
                          android:maxLength="8" />
                  </android.support.design.widget.TextInputLayout>
          </LinearLayout>
      

      【讨论】:

        【解决方案3】:

        我知道它已经有一段时间了,但是将它添加到您的 TextInputLayout 工作:

         app:boxCornerRadiusBottomEnd="20dp"
         app:boxCornerRadiusBottomStart="20dp"
         app:boxCornerRadiusTopEnd="20dp"
         app:boxCornerRadiusTopStart="20dp"
        

        【讨论】:

          【解决方案4】:

          只需使用 Material Components 库和标准 TextInputLayout 组件即可。

          添加 app:boxCornerRadiusBottomEnd="xxdp"app:boxCornerRadiusTopEnd="xxdp"app:boxCornerRadiusBottomStart="xxdp"app:boxCornerRadiusTopStart="xxdp" 属性。

          类似:

              <com.google.android.material.textfield.TextInputLayout
                  style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                  app:endIconMode="password_toggle"
                  app:boxCornerRadiusBottomEnd="8dp"
                  app:boxCornerRadiusTopEnd="8dp"
                  app:boxCornerRadiusBottomStart="8dp"
                  app:boxCornerRadiusTopStart="8dp"
                  ...>
          

          否则您可以定义自定义样式并使用 shapeAppearanceOverlay 属性:

              <com.google.android.material.textfield.TextInputLayout
                  android:id="@+id/custom_end_icon"
                  android:hint="Hint text"
                  style="@style/OutlinedRoundedBox"
                  ...>
          

          与:

            <style name="OutlinedRoundedBox" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
              <item name="shapeAppearanceOverlay">@style/ShapeAppearanceOverlay.MyApp.TextInputLayout.Rounded</item>
            </style>
          
            <style name="ShapeAppearanceOverlay.MyApp.TextInputLayout.Rounded" parent="">
              <item name="cornerFamily">rounded</item>
              <item name="cornerSize">8dp</item>
            </style>
          

          【讨论】:

          • 喜欢定制风格的作品。我也想给盒子添加一些阴影,但没有运气。你也可以帮我吗?谢谢:)
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-04-16
          • 2020-12-30
          • 1970-01-01
          • 2020-05-19
          • 2017-04-27
          • 2021-07-24
          相关资源
          最近更新 更多