【问题标题】:Inconsistent results when nesting multiple LinearLayouts嵌套多个 LinearLayout 时结果不一致
【发布时间】: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">
...

这是一个模拟器上的漂亮用户界面:

以下是屏幕较小的模拟器上的水平不一致:

注意:在上面代码的第二个布局中,我试图设置金色星星的位置以覆盖背景图像中的灰色星星。在第一种情况下它可以工作,但在第二种情况下,如您所见,星星并不完全适合......

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    在 secong LinearLayout 你有:

    android:layout_height="wrap_content"
    

    但由于它是第一个 LinearLayout 的子视图(使用权重):它也必须使用权重并且您必须将其设置为 android:layout_height="0dp"


    查看屏幕截图后,黄色星星似乎有点小(高度),我猜这是因为android:layout_height="wrap_content" 所以尝试设置布局高度以匹配父级:

    <ImageView
           android:layout_width="0dp"
           android:layout_height="match_parent"
           android:layout_weight="45"
           android:contentDescription="@string/star_desc"
           android:src="@drawable/star"/>
    

    【讨论】:

    • 感谢您的回复!事实上,我已经按照你的建议做了,它修复了但只是垂直不一致。横向差异仍然存在。您在我的代码中发现更多问题了吗?
    • 差异不是很大,但很明显,尤其是因为我试图用另一个相同大小的图像覆盖 1 个图像...这可能是 android 错误,因为我真的看不到什么可能错了
    • 放一张你现在拥有的截图可能会有所帮助。
    • uff,这是一个非常烦人的错误检测...问题出在我的图形上。 3 个灰色星和整个对话框是 1 个图像,黄色星看起来更小,因为灰色星与对话框一起被缩放为 1 个图像。黄色星是独立缩放的。这可能导致它们的大小不一致......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-04
    • 2014-09-21
    • 1970-01-01
    相关资源
    最近更新 更多