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

时间:2013-04-06 14:47:03

标签: android android-layout multiscreen

我的定位元素有问题。

我希望拥有的是所有设备中的此图像,无论屏幕尺寸如何。

enter image description here

我所拥有的是两种不同的尺寸

enter image description here enter image description here

我希望textViews和editTexts在活动视图中保持其位置。 (图像中的图像'绿 - 蓝 - 红 - 黄色方形图像')

这是我的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>

我怀疑这个问题与我用来定位元素的填充和边距有关。但我找不到任何其他办法。

有没有一种方法可以建议我的元素在所有屏幕尺寸中保持其位置?

1 个答案:

答案 0 :(得分:0)

为不同的设备制作单独的xml文件,并为两个xml分别定位图像,以适应不同的标准分辨率。为两个XML文件链接两个单独的类,现在基于屏幕分辨率调用两个类中的任何一个。您可以使用以下代码获取屏幕res:

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

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

使用if-else条件并选择意图

的类
相关问题