![](/img/trans.png)
[英]Same position for UI elements (buttons, image buttons) on all android devices
[英]android how to position elements to support in all devices
我对定位元素有疑问。
我想要的是无论屏幕大小如何,所有设备中的这张图片。
我所拥有的是两种不同尺寸的
我希望我的textViews和editTexts在活动视图中保持其位置。 (imageView中的图像“包含图像的绿色,蓝色,蓝色,红色,黄色正方形”)
这是我的layout.xml
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_height="30dp"
android:layout_width="150dp"
android:layout_marginTop="60dp"
android:layout_marginLeft="100dp"
android:textSize="19dp"
android:textStyle="bold"
android:gravity="center"
android:text="@string/login_app_name">
</TextView>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="20dp"
android:layout_marginTop="128dp"
android:gravity="center_vertical">
<TextView
android:layout_height="20dp"
android:layout_width="100dp"
android:layout_marginLeft="35dp"
android:textStyle="bold"
android:gravity="right|center_vertical"
android:text="@string/login_username">
</TextView>
<EditText
android:layout_height="20dp"
android:layout_width="140dp"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:background="@drawable/login_edit_text">
</EditText>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="20dp"
android:layout_marginTop="2dp">
<TextView
android:layout_height="20dp"
android:layout_width="100dp"
android:layout_marginLeft="35dp"
android:textStyle="bold"
android:gravity="right|center_vertical"
android:text="@string/login_password">
</TextView>
<EditText
android:layout_height="20dp"
android:layout_width="140dp"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:background="@drawable/login_edit_text">
</EditText>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
我怀疑问题与我用来定位元素的填充和边距有关。 但是我找不到其他办法。
有什么方法可以建议我的元素在所有屏幕尺寸上保持其位置?
为不同的设备制作单独的xml文件,并为两个xml分别放置图像,以适应不同的标准分辨率。 链接两个XML文件的两个单独的类,现在基于屏幕分辨率调用两个类中的任何一个。 您可以使用以下代码获取屏幕资源:
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
final int height = dm.heightPixels;
final int width = dm.widthPixels;
使用if-else条件并选择要使用的类
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.