所有android设备上的UI元素(按钮、图像按钮)的位置相同
我的应用程序使用通过XML和drawable文件夹构建的按钮和图像按钮。所有android设备上的UI元素(按钮、图像按钮)的位置相同,android,xml,android-layout,devise,imagebutton,Android,Xml,Android Layout,Devise,Imagebutton,我的应用程序使用通过XML和drawable文件夹构建的按钮和图像按钮。 在某些设备上,应用程序UI元素(按钮和图像按钮)失去了位置,彼此重叠,在某些设备上,屏幕底部的最后一个按钮消失。 方向改变时也会发生同样的情况 我希望我的所有元素在所有设备上都位于同一位置。 如何使用XML实现此功能。 有什么简单易行的方法吗? 这是我的XML <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
在某些设备上,应用程序UI元素(按钮和图像按钮)失去了位置,彼此重叠,在某些设备上,屏幕底部的最后一个按钮消失。
方向改变时也会发生同样的情况 我希望我的所有元素在所有设备上都位于同一位置。
如何使用XML实现此功能。
有什么简单易行的方法吗?
这是我的XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg"
android:orientation="vertical" >
<ImageButton
android:id="@+id/main_btn_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="55dp"
android:layout_marginTop="71dp"
android:background="#ffffff"
/>
<ImageButton
android:id="@+id/main_btn_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/main_btn_1"
android:layout_marginRight="56dp"
android:background="#000000"
/>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/main_btn_1"
android:layout_below="@+id/main_btn_1"
android:textColor="#00aeed"
android:textStyle="normal" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_alignLeft="@+id/main_btn_2"
android:textColor="#ea1d24"
android:textStyle="normal" />
<ImageButton
android:id="@+id/main_btn_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="42dp"
android:background="#000000"
/>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/main_btn_3"
android:layout_below="@+id/main_btn_3"
android:textColor="#f7941d"
android:textStyle="normal" />
<ImageButton
android:id="@+id/main_btn_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_alignTop="@+id/main_btn_3"
android:background="#000000"
/>
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/main_btn_4"
android:layout_below="@+id/main_btn_4"
android:textColor="#f7941d"
android:textStyle="normal" />
<ImageButton
android:id="@+id/main_btn_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView4"
android:layout_centerHorizontal="true"
android:layout_marginTop="12dp"
android:background="#000000"
/>
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/main_btn_5"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:textColor="#0065b3"
android:textStyle="normal" />
</RelativeLayout>
因为有许多设备具有如此多不同的屏幕大小、分辨率等,所以在放置布局元素时,可能必须避免使用绝对位置。Android SDK有一些强大的结构来避免绝对定位(即LinearLayout、RelativeLayout),因此尝试使用它们,而不是定义“12dp”等位置,而是使用布局宽度、布局高度(包裹内容或匹配父项)和布局权重的正确组合,这可以帮助您在不指定绝对位置的情况下放置布局元素。您使用分辨率感知图形吗?是的,我使用了,每个图像使用了9个面片在facts中,他使用了一个RelativeLayout,所有位置都是相对的(alignTop、toLeftOf、alignParentRight,…所有相对定位-非绝对)但是他在所有设备中都使用了wrap\u content
,而不是使用layout\u weight
参数,这将有助于规范大多数设备上的布局。我也尝试过这种方法,但无法使其正常工作。就像我希望第一个图像按钮(main_btn_1)是从左边2或5个像素的位置一样,如果没有dp,我就做不到。如果有任何方法,请建议布局重量只能用于线性布局。它一次只能在一个维度上工作(设置为0dp的维度)。所以,要通过权重进行二维重新分布,必须嵌套线性布局。。。真是一团糟!!如果花时间,您可以通过RelativeLayouts进行相同的重新分发,使用较少的版面。版面中的问题是,您在任何地方都使用了wrap\u content
作为layout\u width
。请记住,在不同的设备中有许多不同的宽度,在其中一些设备中,“行”根本不适合,并被包装到下一行中。我将使用带有android:orientation=“horizontal”的线性布局
,并将您希望在一行中显示的元素放在那里,然后使用布局权重设置它们应有的相对宽度。