【问题标题】:adjust EditTexts and buttons with different screen sizes调整不同屏幕尺寸的 EditTexts 和按钮
【发布时间】:2015-03-23 13:15:35
【问题描述】:

如何调整 EditTexts 和 Buttons 以适应 android 的任何屏幕尺寸?我试图将一些透明的 EditTexts 和 Buttons 放在某些位置以适合我的背景图像,但是当我改变屏幕大小时,一切都会改变。这是我的背景图片和我的 XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/login_page"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:padding="10sp" >

    <EditText
        android:id="@+id/etLogin"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10sp"
        android:layout_marginTop="135sp"
        android:background="@android:color/transparent"
        android:hint="Login"
        android:inputType="textCapWords"
        android:padding="8sp" />

    <EditText
        android:id="@+id/etPassword"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="0sp"
        android:layout_marginTop="-3sp"
        android:background="@android:color/transparent"
        android:hint="Password"
        android:inputType="textPassword"
        android:padding="8sp" />

    <Button
        android:id="@+id/btnLogin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="12sp"
        android:background="@android:color/transparent"
        android:minHeight="40sp"
        android:minWidth="500sp"
        android:text="Login"
        android:textColor="#08b0ef" />
</LinearLayout>

我尝试了其他单位,如 dp、dpi 和 dip,但没有人给出预期的结果。

【问题讨论】:

  • 您应该对视图使用 dp,而不是 spsp 仅适用于文本大小。
  • 正如我在问题中所说的,我已经尝试过了。
  • 我的不是解决方案。只是给use the proper units的小费。
  • 因为我不确定adjust EditTexts and Buttons to fit any screen size 是什么意思。
  • 我的意思是我希望 EditTexts 保持在深蓝色区域,而按钮保持在白色区域,无论设备的结果如何。

标签: android xml


【解决方案1】:

您是否尝试过使用“android:layout_weight”? 我认为您所说的将布局对象设置在相对于背景的某个位置,并且一旦设置,就按比例保持固定,这样当背景拉伸我们的合同时,按钮也会如此,以便它们在背景上保持相同的位置?

如果是这样(我可能会离开), 在 LinearLayout 的每个子对象中设置 layout_weight 属性可以让您相对于屏幕尺寸定位所有内容,以便它随着每个屏幕尺寸自动更改。获得正确的百分比需要一些试验和错误,但应该可以工作。

还可以考虑为主要屏幕尺寸创建多个 xml 布局定义,以便操作系统自动调用具有资源限定符的特定屏幕所需的布局,这样您就知道它将显示在正确的位置。例如,默认情况下,R.layout 中名为 activity_main 的 xml 会膨胀,但如果您还在 R.layout-land 中创建了 activity_main,则该 XML 仅在屏幕处于横向模式时才会膨胀。因此,您可以为多种屏幕尺寸设置 editText 和 Buttons 的尺寸。

【讨论】:

    【解决方案2】:

    如果您只想为所有布局创建一个文件并尝试使您的屏幕通用,您应该尝试使用权重属性的 Linearlayout。

    除了使用 sp 或 px,您应该使用 pd 来表示布局中的边距、填充或任何其他属性。 dp 会根据屏幕分辨率呈现不同的效果。

    【讨论】:

      猜你喜欢
      • 2020-01-23
      • 1970-01-01
      • 2014-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-07
      相关资源
      最近更新 更多