EditText高度根据文字大小变化

EditText height changes according to the text size

如果我改变 edittext 中的 textsize,edittext 的高度也会随之改变。我希望 edittext 的大小相同,并且我希望其中包含更小的提示或文本。我应该怎么办?以下是代码,我还发布了此布局的屏幕截图:

  <RelativeLayout
            android:id="@+id/normal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#4594e4"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/set"
                android:layout_width="wrap_content"
                android:layout_height="75dp"
                android:layout_marginTop="05dp"
                android:padding="5dp"
                android:src="@drawable/bari" />

            <EditText
                android:id="@+id/etOrigin"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="@dimen/_35sdp"
                android:layout_marginTop="@dimen/_10sdp"
                android:layout_toEndOf="@+id/set"
                android:layout_toRightOf="@+id/set"
                android:background="#5fa7f1"
                android:inputType="textPersonName"
                android:text="My Location"
                android:textColor="#434343"
                android:textIsSelectable="true" />

            <EditText
                android:id="@+id/etDestination"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/etOrigin"
                android:layout_marginRight="@dimen/_35sdp"
                android:layout_marginTop="@dimen/_5sdp"
                android:layout_toEndOf="@+id/set"
                android:layout_toRightOf="@+id/set"
                android:background="#5fa7f1"
                android:inputType="textPersonName"
                android:onClick="wow"
                android:text="Where to go ?"
                android:textColor="#fff"
                android:textColorHint="#fff" />

            <ImageView
                android:id="@+id/swipe"
                android:layout_width="30dp"
                android:layout_height="27dp"
                android:layout_alignParentEnd="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="@dimen/_5sdp"
                android:background="@drawable/flip" />

        </RelativeLayout>

更改前的布局截图:

更改文本大小后的布局屏幕截图:

为布局提供固定高度 android:layout_height="75dp"

            <EditText
            android:id="@+id/etOrigin"
            android:layout_width="match_parent"
            android:layout_height="75dp"
            android:layout_marginRight="@dimen/_35sdp"
            android:layout_marginTop="@dimen/_10sdp"
            android:layout_toEndOf="@+id/set"
            android:layout_toRightOf="@+id/set"
            android:background="#5fa7f1"
            android:inputType="textPersonName"
            android:text="My Location"
            android:textColor="#434343"
            android:textIsSelectable="true"/>                  

为此使用填充:-

         <EditText
            android:id="@+id/etOrigin"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="12dp"
            android:layout_marginRight="@dimen/_35sdp"
            android:layout_marginTop="@dimen/_10sdp"
            android:layout_toEndOf="@+id/set"
            android:layout_toRightOf="@+id/set"
            android:background="#5fa7f1"
            android:inputType="textPersonName"
            android:text="My Location"
            android:textColor="#434343"
            android:textIsSelectable="true" />

尝试使用 android:minHeightandroid:maxHeight 属性:

                            <EditText
                            android:id="@+id/etUserName"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:hint="@string/hint_username"
                            android:imeOptions="actionNext"
                            android:inputType="textPersonName"
                            android:maxLines="1"
                            android:gravity="center_vertical"
                            android:minHeight="?actionBarSize"
                            android:maxHeight="?actionBarSize"
                            android:textColor="@color/textPrimary"
                            android:textSize="12sp"/>

                            <EditText
                            android:id="@+id/etUserEmail"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:hint="@string/hint_username"
                            android:imeOptions="actionNext"
                            android:inputType="textEmailAddress"
                            android:maxLines="1"
                            android:gravity="center_vertical"
                            android:minHeight="?actionBarSize"
                            android:maxHeight="?actionBarSize"
                            android:textColor="@color/textPrimary"
                            android:textSize="14sp"/>

您可以明确设置 EditText 视图的高度和大小以及其中的文本。

要设置 EditText 的高度,只需使用:

android:layout_height="<height_you_want>dp"

并设置 EditText 视图中文本的高度:

android:textSize="<height_you_want>sp"

并且您可以同时在AndroidStudio的预览选项卡中查看变化。

在这里,试试这个第三方库。 您的 TextView 高度将是固定的,但您的 textView 大小将被调整

dependencies {
    compile 'me.grantland:autofittextview:0.2.+'
}

在代码中启用任何扩展 TextView 的视图:

AutofitHelper.create(textView);

在 XML 中启用任何扩展 TextView 的视图:

<me.grantland.widget.AutofitLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:singleLine="true"
        />
</me.grantland.widget.AutofitLayout>

在代码中使用内置的 Widget 或 XML:

<RootElement
    xmlns:autofit="http://schemas.android.com/apk/res-auto"
    ...
<me.grantland.widget.AutofitTextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:maxLines="2"
    android:textSize="40sp"
    autofit:minTextSize="16sp"
    />

如需了解更多信息,请访问下方 link:

https://github.com/grantland/android-autofittextview

一个解决方案:在您的 XML 文件中包含这两行

android:maxLines="1" android:singleLine="真"

ConstraintLayout 中使用 EditText 可以解决这个问题。

...
<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="17">
    <androidx.appcompat.widget.AppCompatEditText
        android:layout_width="match_parent"
        android:layout_height="0dp"                        
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHeight_percent="1"
        android:textSize="24sp"
        android:text="foo"
    />
</androidx.constraintlayout.widget.ConstraintLayout>