草庐IT

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

coder 2023-11-23 原文

更新#1

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

TLDR 版本

你好,

我的 Android 项目中输入元素的大小存在问题。输入元素在尺寸 <>

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

详细版

你好,

我目前在不同模拟器上的 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>

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

有关Android 表单元素在不同的模拟器上有不同的大小的更多相关文章

  1. ruby-on-rails - 在 Rails 中将文件大小字符串转换为等效千字节 - 2

    我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,

  2. ruby-on-rails - Rails 编辑表单不显示嵌套项 - 2

    我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib

  3. ruby - 如何模拟 Net::HTTP::Post? - 2

    是的,我知道最好使用webmock,但我想知道如何在RSpec中模拟此方法:defmethod_to_testurl=URI.parseurireq=Net::HTTP::Post.newurl.pathres=Net::HTTP.start(url.host,url.port)do|http|http.requestreq,foo:1endresend这是RSpec:let(:uri){'http://example.com'}specify'HTTPcall'dohttp=mock:httpNet::HTTP.stub!(:start).and_yieldhttphttp.shou

  4. ruby - 如何在 Rails 4 中使用表单对象之前的验证回调? - 2

    我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser

  5. ruby - 在哈希的键数组中追加元素 - 2

    查看我的Ruby代码:h=Hash.new([])h[0]=:word1h[1]=h[1]输出是:Hash={0=>:word1,1=>[:word2,:word3],2=>[:word2,:word3]}我希望有Hash={0=>:word1,1=>[:word2],2=>[:word3]}为什么要附加第二个哈希元素(数组)?如何将新数组元素附加到第三个哈希元素? 最佳答案 如果您提供单个值作为Hash.new的参数(例如Hash.new([]),完全相同的对象将用作每个缺失键的默认值。这就是您所拥有的,那是你不想要的。您可以改用

  6. 「Python|Selenium|场景案例」如何定位iframe中的元素? - 2

    本文主要介绍在使用Selenium进行自动化测试或者任务时,对于使用了iframe的页面,如何定位iframe中的元素文章目录场景描述解决方案具体代码场景描述当我们在使用Selenium进行自动化测试的时候,可能会遇到一些界面或者窗体是使用HTML的iframe标签进行承载的。对于iframe中的标签,如果直接查找是无法找到的,会抛出没有找到元素的异常。比如近在咫尺的例子就是,CSDN的登录窗体就是使用的iframe,大家可以尝试通过F12开发者模式查看到的tag_name,class_name,id或者xpath来定位中的页面元素,会抛出NoSuchElementException异常。解决

  7. HBase Region 简介和建议数量&大小 - 2

    Region是HBase数据管理的基本单位,region有一点像关系型数据的分区。region中存储这用户的真实数据,而为了管理这些数据,HBase使用了RegionSever来管理region。Region的结构hbaseregion的大小设置默认情况下,每个Table起初只有一个Region,随着数据的不断写入,Region会自动进行拆分。刚拆分时,两个子Region都位于当前的RegionServer,但处于负载均衡的考虑,HMaster有可能会将某个Region转移给其他的RegionServer。RegionSplit时机:当1个region中的某个Store下所有StoreFile

  8. 安卓apk修改(Android反编译apk) - 2

    最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路

  9. java - 为什么 ruby​​ modulo 与 java/other lang 不同? - 2

    我基本上来自Java背景并且努力理解Ruby中的模运算。(5%3)(-5%3)(5%-3)(-5%-3)Java中的上述操作产生,2个-22个-2但在Ruby中,相同的表达式会产生21个-1-2.Ruby在逻辑上有多擅长这个?模块操作在Ruby中是如何实现的?如果将同一个操作定义为一个web服务,两个服务如何匹配逻辑。 最佳答案 在Java中,模运算的结果与被除数的符号相同。在Ruby中,它与除数的符号相同。remainder()在Ruby中与被除数的符号相同。您可能还想引用modulooperation.

  10. ruby-on-rails - 在 RSpec 中,如何以任意顺序期望具有不同参数的多条消息? - 2

    RSpec似乎按顺序匹配方法接收的消息。我不确定如何使以下代码工作:allow(a).toreceive(:f)expect(a).toreceive(:f).with(2)a.f(1)a.f(2)a.f(3)我问的原因是a.f的一些调用是由我的代码的上层控制的,所以我不能对这些方法调用添加期望。 最佳答案 RSpecspy是测试这种情况的一种方式。要监视一个方法,用allowstub,除了方法名称之外没有任何约束,调用该方法,然后expect确切的方法调用。例如:allow(a).toreceive(:f)a.f(2)a.f(1)

随机推荐