android如何定位元素以支持所有设备
问题描述:
我有定位元素的问题。android如何定位元素以支持所有设备
我想要的是这个图像在所有设备上,不管屏幕尺寸是多少。
我有什么,这是两种不同的大小
我希望我的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条件并选择类意图