编辑文本的默认高度是多少?

What is the default height of a edittext?

在我的 xml 代码中,有一个微调器和编辑文本的高度都设置为 wrap_content。但是微调器的高度小于编辑文本。我的目标是将两者的高度设置为相同。

我尝试将 Widget.AppCompact.EditText 样式应用于微调器,但它不起作用。我目前使用 Theme.AppCompat.Light.DarkActionBar 作为基本主题。

要具有相同的高度,请像这样使用 LinearLayoutlayout_weight

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
    <Spinner
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
</LinearLayout>

ConstraintLayout

很容易达到你想要的效果
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/edit_text"
        android:layout_width="200dp" // Change to what width you prefer
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"/>

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="@id/edit_text"
        app:layout_constraintBottom_toBottomOf="@id/edit_text"
        app:layout_constraintStart_toEndOf="@id/edit_text"/>

</androidx.constraintlayout.widget.ConstraintLayout>

您可以在 onCreate 方法中执行此操作:

editText.post(new Runnable() {
        @Override
        public void run() {
            ViewGroup.LayoutParams layoutParams=spinner.getLayoutParams();
            layoutParams.height=editText.getHeight();
            spinner.layoutParams=layoutParams;
        }
    });