【发布时间】:2014-09-04 19:59:28
【问题描述】:
问题在于,在不同的屏幕尺寸下,下面代码中的 2 个 ImageView 位于稍微不同的位置。我不知道出了什么问题,理论上一切都应该在所有屏幕上显示相同,因为我正在使用 layout_weights...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/menu_play_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bg_color" >
<!-- relative layout contains 2 nested LinearLayouts here -->
<RelativeLayout
android:id="@+id/game_result_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- First LinearLayout -->
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:weightSum="600">
<View android:layout_weight="99" android:layout_width="match_parent" android:layout_height="0dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="75"
android:contentDescription="@string/star_desc"
android:src="@drawable/victory"/>
<View android:layout_weight="257" android:layout_width="match_parent" android:layout_height="0dp"/>
<!-- Second LinearLayout -->
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1024">
<View android:layout_weight="538" android:layout_width="0dp" android:layout_height="match_parent"/>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="45"
android:contentDescription="@string/star_desc"
android:src="@drawable/star"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
但是,只有在添加第二个嵌套的 LinearLayout 后才会出现问题。只有第一个 LinearLayout,那里的 ImageView 显示在所有屏幕的正确位置。像上面那样嵌套第二个 LinearLayout 后,两个 ImageView 在不同屏幕上的位置都会略有变化。
更新: Ben75 提供的解决方案修复了垂直不一致,但仍然存在一些小的水平差异。 这是更新的第二个(嵌套)LinearLayout 代码:
...
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="40"
android:weightSum="1024">
...
这是一个模拟器上的漂亮用户界面:
以下是屏幕较小的模拟器上的水平不一致:
注意:在上面代码的第二个布局中,我试图设置金色星星的位置以覆盖背景图像中的灰色星星。在第一种情况下它可以工作,但在第二种情况下,如您所见,星星并不完全适合......
【问题讨论】: