android 工作室预览尺寸与实际尺寸不一样

android studio preview size not same as actual size

在 android studio 中,我有一个 Spinner 和一个文本字段,采用相同的线性布局。它们的权重都为 1。在 android 预览中,我得到了这个, spinner and text field are the same size

但是当我加载我的设备时,我实际上得到 the spinner tiny and the text field big

这是我的代码

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="2"
    android:id="@id/linearLayout">

    <Spinner
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/spinner"
        android:spinnerMode="dialog"
        android:entries="@array/State"
        android:layout_marginTop="110sp"
        android:layout_weight="1"/>

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:ems="999999999"
        android:layout_marginTop="130sp"
        android:layout_weight="1"/>

</LinearLayout>

试试这个:

<Spinner
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:id="@+id/spinner"
    android:spinnerMode="dialog"
    android:entries="@array/State"
    android:layout_marginTop="110sp"
    android:layout_weight="1"/>

<EditText
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal"
    android:ems="999999999"
    android:layout_marginTop="130sp"
    android:layout_weight="1"/>

注意 android:layout_width="0dp".

让 ART 使用权重根据您的 layout:weight 值分配字段。

删除此行

 android:layout_marginTop="130sp"

它创造了最高利润率。

有些时候预览需要刷新 文本和设计之间的切换