简体   繁体   English

Android Material TextInputLayout endIcon 截断 MaterialAutoCompleteTextView 中的文字

[英]Android Material TextInputLayout endIcon cuts off text in MaterialAutoCompleteTextView

I have some very short text input (like only 2 numeric characters) yet using the endIcon will hog half of my textview and will cut off and display ellipses.我有一些非常短的文本输入(比如只有 2 个数字字符),但使用 endIcon 会占用我的文本视图的一半,并且会切断并显示省略号。 I cannot make the TextInputLayout wider so I how can I display the full 2 characters (or more) while using endIconDrawable?我无法使 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>

It is a workaround, not a final solution.这是一种解决方法,而不是最终解决方案。

You can reduce the padding between the text and endIcon by adding a negative value in android:drawablePadding .您可以通过在android:drawablePadding中添加负值来减少文本和endIcon之间的填充。
Something like:就像是:

   <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"
        ../>

在此处输入图像描述

A final note.最后一点。 You don't need to use android:background="@drawable/text_input_assessment" to have rounded corners.您不需要使用android:background="@drawable/text_input_assessment"来获得圆角。 Just add app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.Rounded" in the TextInputLayout with:只需在TextInputLayout中添加app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.Rounded"

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

You've defined a style for it already你已经为它定义了一个样式

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

So, the above line will add the dropdown icon automatically.因此,上面的行将自动添加下拉图标。 I think you'll need to remove this line我认为您需要删除此行

app:endIconDrawable="@drawable/ptx_down_arrow_android"

Maybe the above line is overlapping the default icon也许上面的行与默认图标重叠

Then if you want to set tint to the drawable, just create a custom style然后,如果您想为drawable设置色调,只需创建一个自定义样式

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

And you add this two lines if you like如果你愿意,你可以添加这两行

style="@style/CustomAutoCompleteLayout"

android:theme="@style/CustomAutoCompleteLayout"

Also i suggest that you remove this line and add it to the com.google.android.material.textfield.TextInputLayout instead.另外我建议您删除此行并将其添加到com.google.android.material.textfield.TextInputLayout中。

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

Okay, i created this just now after 2hours and it worked very fine for me..好的,我刚刚在 2 小时后创建了这个,它对我来说效果很好..

styles.xml样式.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>

Don't worry about the constans eg @color/colorPrimary.不要担心常量,例如@color/colorPrimary。 What matters is the parent theme.重要的是父主题。

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请记住在 android_manifest.xml 中将其用作 AppTheme 的父主题,因为您已将 style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense.ExposedDropdownMenu 添加到 <com.google.android.material.textfield.TextInputLayout否则应用程序崩溃

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

Then add this to styles.xml also然后将其添加到 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>

You can now modify your activity_main.xml您现在可以修改您的 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您只需要小心填充并记住 @dimen/_6dp 的值为 6dp 而@dimen/_2dp 的值为 2dp

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

to

<AutoCompleteTextView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android TextInputEditText/TextInputLayout 切断错误信息 - Android TextInputEditText/TextInputLayout cuts off error message Android 材质 TextInputLayout 和 TextInputEditText - Android Material TextInputLayout and TextInputEditText 如何在 Android Material TextInputLayout 中将 SpannableString 设置为 EditText? - How to set a SpannableString to an EditText in an Android Material TextInputLayout? AES加密会在CFB模式下切断文本 - AES encryption cuts off text in CFB mode 一段后,Android TextView中断 - Android TextView cuts off after one paragraph 绘制JLabel会截断HTML文本 - Painting JLabel cuts off HTML Text 如何去除Android Material Design中TextInputLayout下的下划线? - How to remove the underline under TextInputLayout in Android Material Design? 如何在TextInputLayout中设置错误endicon,该错误在出现错误时显示,在没有错误时消失 - How to set error endicon in TextInputLayout.that show when error occur and disappear when there is no error Android Studio - 如何在 Java 中以编程方式为 TextInputLayout 设置文本? - Android Studio - How to set text for TextInputLayout programmatically in Java? MediaRecorder切断文件结尾 - MediaRecorder cuts off end of file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM