如何调整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,但没有人给出预期的结果。
答案 0 :(得分:0)
您是否尝试过使用“android:layout_weight”? 我认为你所说的是将布局对象设置在相对于背景的某个位置并且一旦设置,按比例保持固定,这样当背景拉伸我们的合同时,按钮使得它们在背景上保持相同的位置?
如果是这样(我可能会离开), 在LinearLayout的每个子对象中设置layout_weight属性可以让您定位与屏幕大小相关的所有内容,使其随每个屏幕大小自动更改。要获得正确的百分比需要一些试验和错误,但是应该有效。
还要考虑为主要屏幕尺寸创建多个xml布局定义,以便操作系统使用资源限定符自动调用特定屏幕所需的那个,这样您就知道它将显示在正确的位置。例如,默认情况下,R.layout中名为activity_main的xml会膨胀,但如果您还在R.layout-land中创建了activity_main,则只有在屏幕处于横向模式时才会使该XML膨胀。因此,您可以为多个屏幕尺寸设置editText和按钮的大小。
答案 1 :(得分:0)
如果您只想为所有版面创建一个文件并尝试使屏幕具有通用性,那么您应该尝试使用权重属性的Linearlayout。
您应该使用pd作为边距,填充或布局中的任何其他属性,而不是使用sp或px。 dp将根据屏幕分辨率进行不同的渲染。