【问题标题】:Nested Weight In XML Linear LayoutXML线性布局中的嵌套权重
【发布时间】:2014-04-25 13:48:11
【问题描述】:

这是我的布局,我收到一条警告:"Nested weights are bad for performance" for ImageView。我怎样才能避免这种情况,我做错了什么?

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@android:color/white">


<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:orientation="horizontal"
    android:layout_weight="1.50">

    <ImageView 
        android:layout_width="0dp"
        android:layout_height="60dp"
        android:background="@drawable/left_top"
        android:layout_weight=".75"  
        android:contentDescription="@string/title" 
        />
     <ImageView 
        android:layout_width="0dp"
        android:layout_height="60dp"
        android:layout_weight="1.25"
        android:contentDescription="@string/title" 
        />

    <ImageView
        android:layout_width="0dp"
        android:layout_height="60dp"
        android:background="@drawable/right_top" 
        android:layout_weight="0.50"
        android:contentDescription="@string/title"  />

</LinearLayout>


    <ImageView
        android:id="@+id/imagefirstname"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="3"
        android:contentDescription="@string/app_name"
        android:background="@drawable/logo"
        android:scaleType="center"
        android:layout_gravity="center_horizontal" 
        />


        <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="@string/welcome_title"
        android:textSize="25sp"
        android:layout_gravity="center"
        android:gravity="center"
         /> 

    <LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:orientation="horizontal"
    android:layout_weight=".75">

    <ImageView 
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="@drawable/left_bottom"
        android:layout_weight=".75"
        android:contentDescription="@string/title" 
        />
     <ImageView 
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1.25"
        android:contentDescription="@string/title" 
        />

    <ImageView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="@drawable/right_bottom" 
        android:layout_weight="0.50"
        android:contentDescription="@string/title"  />

</LinearLayout>



</LinearLayout>

我可以知道实现目标的正确方法是什么吗?也许这个问题太基础了,但我没有找到任何合适的解决方案。

请帮帮我。

【问题讨论】:

标签: android xml android-linearlayout android-layout-weight


【解决方案1】:

根据 Android 文档:

使用 LinearLayout 的嵌套实例会导致过深 查看层次结构。嵌套几个使用 layout_weight 参数可能特别昂贵,因为每个孩子 需要测量两次。这一点尤其重要,当 布局反复膨胀,例如在 ListView 中使用时或 网格视图。

进一步 The performance might improve by flattening the layout—make the layout shallow and wide, rather than narrow and deep.

你有一个 LinearLayout 在另一个里面有布局权重。而不是你可以使用RelativeLayout 代替LinearLayout

请阅读 docs 了解更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-21
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多