简体   繁体   English

Android如何定位元素以支持所有设备

[英]android how to position elements to support in all devices

I have a problem with positioning elements. 我对定位元素有疑问。

What I want to have is this image in all devices no matter screen size is. 我想要的是无论屏幕大小如何,所有设备中的这张图片。

在此处输入图片说明

what I have is this in two different sizes 我所拥有的是两种不同尺寸的

在此处输入图片说明在此处输入图片说明

I want my textViews and editTexts maintain their positions in the activity view. 我希望我的textViews和editTexts在活动视图中保持其位置。 (The image 'Green-Blue-Red-Yellow square containing image' in an imageView) (imageView中的图像“包含图像的绿色,蓝色,蓝色,红色,黄色正方形”)

and this is my layout.xml 这是我的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>

I suspect the problem is related to tha paddings and margins that I used to position elements. 我怀疑问题与我用来定位元素的填充和边距有关。 But I couldn't find any other way around. 但是我找不到其他办法。

Is there a way that you can suggest for my elements to maintain their positions in all screen sizes? 有什么方法可以建议我的元素在所有屏幕尺寸上保持其位置?

Make separate xml files for the different devices and position the images separately for the two xml's, so as to suit the different standard resolutions. 为不同的设备制作单独的xml文件,并为两个xml分别放置图像,以适应不同的标准分辨率。 link two separate classes for the two XML files, and now based on the screen resolution call either of the two classes. 链接两个XML文件的两个单独的类,现在基于屏幕分辨率调用两个类中的任何一个。 You can get screen res with this code: 您可以使用以下代码获取屏幕资源:

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);

final int height = dm.heightPixels;
final int width = dm.widthPixels;

Use an if-else condition and choose the class to intent to 使用if-else条件并选择要使用的类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 所有Android设备上的UI元素(按钮,图像按钮)的位置相同 - Same position for UI elements (buttons, image buttons) on all android devices 如何在android中创建一个应该支持所有设备的布局? - How to create a layout in android which should support all the devices? 如何在Android中支持所有设备(移动设备和平板电脑)? - How to support all devices( Both mobile and Tablets) in android? 如何使用一个图像支持Android中的所有屏幕和密度设备 - How to use One Image for support all screen and density devices in Android 如何在Android中构建支持所有屏幕尺寸和设备的UI - How to build UI that support all screen sizes and devices in android 如何在支持所有设备的 Android 中创建登录表单? - How to create a login form in Android with Support All Devices? 如何在Android中保持所有设备尺寸的图像位置和大小? - how to maintain the Image Position and Size for all size of Devices in android? 如何将图像定位在所有android设备的同一位置? - How to position images at the same spot all android devices? Android NDK 构建以支持所有可用设备 - Android NDK build to support all available devices Android:对所有设备的多屏支持 - Android : Multiple Screen Support for All Devices
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM