【问题标题】:What is the default height of a edittext?编辑文本的默认高度是多少?
【发布时间】:2019-10-24 15:28:06
【问题描述】:

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

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

【问题讨论】:

    标签: android android-edittext spinner


    【解决方案1】:

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

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

    【讨论】:

    • Thanx 完美运行.. 有什么办法可以通过 xml 做吗?
    【解决方案2】:

    要具有相同的高度,请使用 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>
    

    【讨论】:

    • 它占据了半屏..我只想将默认的edittext高度设置为spinner
    【解决方案3】:

    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>
    

    【讨论】:

    • 我正在使用 schemas.android.com/apk/res/android" android:shape="rectangle"> 作为编辑文本和微调器的背景,但是它们看起来不一样。
    猜你喜欢
    • 2010-10-10
    • 1970-01-01
    • 2016-02-13
    • 2019-09-22
    • 2015-08-12
    • 2011-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多