Android表单元素在不同的模拟器上具有不同的大小

Lai*_*Chu 9 android android-layout

更新#1

我观察到<item name="android:textSize">14sp</item>在/res/values/styles.xml 中设置为20sp将导致输入元素在小于5"的设备上正常显示.似乎减小文本大小并没有减小输入框的大小.这是假设要正常吗?

TLDR版本

嗨,您好,

我在Android项目中遇到输入元素大小的问题.输入元素在大小<5"的设备上显得更大,但在我的大型设备上显得正常.

以下屏幕截图说明了这一点.知道可能是什么原因?

详细版本

嗨,您好,

我目前面临着一个奇怪的问题,我的Android项目中的表单元素的大小跨越不同的模拟器.由于这是我第一次创建移动应用程序,如果这是一个"功能",请随时启发我,而不是一个错误.

Nexus 7布局预览中,表单元素按预期显示:

在此输入图像描述

但是,在Galaxy Nexus布局预览中,表单元素看起来比预期的要大,这是不可取的:

在此输入图像描述

我尝试了很多方法,但一直无法解决这个问题.所有小于5英寸屏幕尺寸的设备都会出现此问题.

这是我的布局XML文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/register_link"
        style="@style/FormFont.Label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="3dp"
        android:clickable="true"
        android:onClick="onClickRegisterLink"
        android:text="@string/register_link_text" />

    <RelativeLayout
        android:id="@+id/layout_layout_form"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_above="@+id/register_link"
        android:gravity="center" >

        <ImageView
            android:id="@+id/logo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/layout_form"
            android:layout_alignRight="@+id/layout_form"
            android:layout_marginBottom="50dp"
            android:contentDescription="@string/logo_description"
            android:src="@drawable/logo" />

        <RelativeLayout
            android:id="@+id/layout_form"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/logo"
            android:gravity="center" >

            <TextView
                android:id="@+id/label_username"
                style="@style/FormFont.Label"
                android:layout_alignBottom="@+id/input_username"
                android:layout_alignLeft="@+id/label_password"
                android:layout_alignRight="@+id/label_password"
                android:layout_alignTop="@+id/input_username"
                android:layout_toLeftOf="@+id/input_username"
                android:gravity="right"
                android:text="@string/label_username" />

            <TextView
                android:id="@+id/label_password"
                style="@style/FormFont.Label"
                android:layout_alignBottom="@+id/input_password"
                android:layout_alignTop="@+id/input_password"
                android:layout_below="@+id/label_username"
                android:layout_marginRight="50dp"
                android:layout_toLeftOf="@+id/input_password"
                android:gravity="right"
                android:text="@string/label_passphrase" />

            <EditText
                android:id="@+id/input_username"
                style="@style/FormFont.Input"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:inputType="text" />

            <EditText
                android:id="@+id/input_password"
                style="@style/FormFont.Input"
                android:layout_alignLeft="@+id/input_username"
                android:layout_alignRight="@+id/input_username"
                android:layout_below="@+id/input_username"
                android:layout_marginTop="30dp"
                android:inputType="textPassword" />

            <Button
                android:id="@+id/button_login"
                style="@style/FormFont.Label"
                android:layout_alignLeft="@+id/input_password"
                android:layout_alignRight="@+id/input_password"
                android:layout_below="@+id/input_password"
                android:layout_marginTop="35dp"
                android:gravity="center"
                android:text="@string/button_login" />
        </RelativeLayout>

    </RelativeLayout>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

我的/res/values/styles.xml:

<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

    <style name="FormFont" parent="@android:style/TextAppearance.Medium">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:gravity">center_vertical</item>
        <item name="android:textSize">14sp</item>
    </style>

    <style name="FormFont.Label">
    </style>

    <style name="FormFont.Input">
        <item name="android:ems">12</item>
    </style>

</resources>
Run Code Online (Sandbox Code Playgroud)

我的/res/values-large/styles.xml:

<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

    <style name="FormFont" parent="@android:style/TextAppearance.Medium">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:gravity">center_vertical</item>
        <item name="android:textSize">20sp</item>
    </style>

    <style name="FormFont.Label">
    </style>

    <style name="FormFont.Input">
        <item name="android:ems">12</item>
    </style>

</resources>
Run Code Online (Sandbox Code Playgroud)

如果您之前遇到此问题,请告诉我,并了解可能是什么原因:)非常感谢!

Lai*_*Chu 1

我通过为, , ,android:layout_height中的属性指定不同的值解决了这个问题。/res/values-xlarge/styles.xml/res/values-large/styles.xml/res/values/styles.xml/res/values-small/styles.xml

这使我能够控制不同尺寸的 Android 设备中每个输入字段的精确高度。