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

标签 android android-layout

更新#1

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

TLDR 版本

你好,

我的 Android 项目中输入元素的大小存在问题。输入元素在尺寸 < 5"的设备上显得更大,但在我的较大设备上显得正常。

下面的截图说明了这一点。知道可能是什么原因吗?

详细版

你好,

我目前在不同模拟器上的 Android 项目中遇到表单元素大小的奇怪问题。 由于这是我第一次创建移动应用程序,如果这是一个“功能”而不是错误,请随时赐教。

Nexus 7 布局预览中,表单元素如我所料出现:

enter image description here

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

enter image description here

我试过很多方法,都无法解决这个问题。所有屏幕尺寸小于 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>

我的/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>

我的/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>

如果您以前遇到过这种情况并了解可能的原因,请告诉我 :) 非常感谢!

最佳答案

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

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

关于Android 表单元素在不同的模拟器上有不同的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16600213/

相关文章:

java - Android - 调整微调器以不选择任何内容或在第一选择时不执行任何操作

android - 数据库调用使 Android 应用程序崩溃

android - 如何在 ADT 的图形布局编辑器中调试自定义 View

java - 如何在 Picasso 的帮助下获取存储在 Firebase 数据库中的图像

java - 如果服务是在客户端应用程序之后安装的,我的应用程序无法连接到另一个应用程序服务

android - 即使数据没有变化,LiveData 观察者的 onChanged 也会在 Activity 轮换时被调用

android - Android View 中的 setMinHeight 和 setMinimumHeight 有什么区别?

Android LayoutParams 未正确更新

android - Android 中复杂的自定义按钮状态

android - 如何从java代码创建EditText数字(有符号和无符号十进制)