Android Material TextInputLayout endIcon 截断 MaterialAutoCompleteTextView 中的文本

Android Material TextInputLayout endIcon cuts off text in MaterialAutoCompleteTextView

我有一些非常短的文本输入(比如只有 2 个数字字符),但使用 endIcon 会占据我的文本视图的一半,并且会截断并显示省略号。我不能使 TextInputLayout 更宽,所以我如何在使用 endIconDrawable 时显示完整的 2 个字符(或更多)?

          <com.google.android.material.textfield.TextInputLayout
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense.ExposedDropdownMenu"
                android:layout_width="80dp"
                android:layout_marginEnd="8dp"
                android:layout_marginStart="8dp"
                android:layout_height="40dp"
                android:background="@drawable/text_input_assessment"
                app:boxStrokeWidth="0dp"
                app:boxStrokeWidthFocused="0dp"
                app:endIconDrawable="@drawable/ptx_down_arrow_android"
                app:endIconTint="@color/colorPtxDarkBlue">

                <com.google.android.material.textfield.MaterialAutoCompleteTextView
                    android:id="@+id/dropdownDay"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@null"
                    android:fontFamily="@font/truenorg"
                    android:textSize="15sp"
                    app:autoSizeTextType="uniform"
                    app:autoSizeMinTextSize="10sp"
                    app:autoSizeMaxTextSize="15sp"
                    app:autoSizeStepGranularity="1sp"
                    android:inputType="none"
                    android:maxLines="1"
                    android:paddingTop="10dp"
                    android:paddingBottom="10dp"
                    android:singleLine="true"
                    android:text="12"
                    android:textAlignment="viewStart"
                    android:textColor="@color/colorPtxDarkBlue"
                    tools:ignore="LabelFor" />

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

这是一种变通方法,不是最终解决方案。

您可以通过在 android:drawablePadding 中添加负值来减少文本与 endIcon 之间的填充。
类似于:

   <com.google.android.material.textfield.MaterialAutoCompleteTextView
        android:id="@+id/dropdownDay"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="8dp"
        android:paddingBottom="8dp"
        android:drawablePadding="-12dp"
        ../>

最后一点。您不需要使用 android:background="@drawable/text_input_assessment" 来获得圆角。只需在 TextInputLayout 中添加 app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.Rounded" 即可:

  <style name="ShapeAppearanceOverlay.Rounded" parent="">
    <item name="cornerSize">50%</item>
  </style

您已经为其定义了样式

style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense.ExposedDropdownMenu"

所以,上面一行会自动添加下拉图标。 我想你需要删除这一行

app:endIconDrawable="@drawable/ptx_down_arrow_android"

也许上面的行与默认图标重叠

然后如果你想为drawable设置色调,只需创建一个自定义样式

<style name="CustomAutoCompleteLayout" parent="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense.ExposedDropdownMenu">
    <item name="endIconTint">@color/colorPtxDarkBlue</item>
</style>

如果你愿意,你可以添加这两行

style="@style/CustomAutoCompleteLayout"

android:theme="@style/CustomAutoCompleteLayout"

此外,我建议您删除此行并将其添加到 com.google.android.material.textfield.TextInputLayout

android:paddingTop="10dp"               
android:paddingBottom="10dp

好的,我刚刚在 2 小时后创建了这个,对我来说效果很好..

styles.xml

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="backgroundTint">@android:color/white</item>
    <item name="colorPrimaryVariant">@color/colorPrimaryDark</item>
    <item name="colorOnPrimary">@color/white</item>
    <!-- Secondary brand color. -->
    <item name="colorSecondary">@color/colorSecondary</item>
    <item name="colorSecondaryVariant">@color/colorSecondaryVariant</item>
    <item name="colorOnSecondary">@color/white</item>
    <item name="android:textColor">@android:color/black</item>
    <item name="button_textcolor">@android:color/black</item>
    <item name="android:textColorPrimary">@android:color/black</item>
    <!-- Status bar color. -->
    <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
    <!-- Customize your theme here. -->
</style>

不用担心常量,例如@color/colorPrimary。重要的是父主题。

Remember to use this below as your parent theme for AppTheme in android_manifest.xml since you've added style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense.ExposedDropdownMenu to <com.google.android.material.textfield.TextInputLayout else app crashes

parent="Theme.MaterialComponents.Light.NoActionBar">

然后将此添加到 styles.xml 也

<style name="AppTheme.MaterialTextInputExposed" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense.ExposedDropdownMenu">
    <item name="hintTextColor">@color/colorSecondary</item>
    <item name="boxStrokeColor">@color/colorSecondary</item>
    <item name="boxStrokeErrorColor">@android:color/holo_red_dark</item>
    <item name="errorTextColor">@android:color/holo_red_dark</item>
    <item name="boxStrokeWidth">@dimen/_2dp</item>
    <item name="boxCornerRadiusBottomEnd">@dimen/_6dp</item>
    <item name="boxCornerRadiusBottomStart">@dimen/_6dp</item>
    <item name="boxCornerRadiusTopStart">@dimen/_6dp</item>
    <item name="boxCornerRadiusTopEnd">@dimen/_6dp</item>
    <item name="colorControlActivated">@color/colorSecondary</item>
    <item name="android:textColor">@android:color/black</item>
    <item name="button_textcolor">@android:color/black</item>
    <item name="endIconTint">@color/spinner</item>
</style>

您现在可以修改您的 activity_main.xml

<com.google.android.material.textfield.TextInputLayout
                    android:id="@+id/choose_admission_mode"
                    style="@style/AppTheme.MaterialTextInputExposed"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/_10dp"
                    android:theme="@style/AppTheme.MaterialTextInputExposed"
                    app:hintEnabled="true"
                    app:layout_constraintBottom_toTopOf="@id/choose_faculty"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@id/choose_program">

                    <AutoCompleteTextView
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:gravity="start"
                        android:imeOptions="actionNext"
                        android:inputType="text"
                        android:textSize="@dimen/_18sp" 

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

I Changed the below line but it's not necessary. You just need to be careful with the padding and remember @dimen/_6dp has value of 6dp and @dimen/_2dp has value of 2dp

<com.google.android.material.textfield.MaterialAutoCompleteTextView

<AutoCompleteTextView