[英]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.