【问题标题】:Layout Weight warning Nested weight bad performance [duplicate]布局权重警告嵌套权重性能不佳[重复]
【发布时间】:2013-01-21 06:22:32
【问题描述】:

我是安卓新手。现在我收到嵌套重量性能不佳的警告。我的第一个屏幕中只有一个图像和三个按钮。 PL 给我一些想法。


<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:background="@color/backgroundColor"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:weightSum="10"
    >


    <ImageView
        android:layout_weight="4"
        android:id="@+id/imageLogo"
        android:layout_width="wrap_content" 
        android:layout_height="0dip"
        android:contentDescription="@string/Img"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="20dp"
        android:src="@drawable/home_screen_logo" />

    <LinearLayout
       android:layout_weight="6"
       android:layout_width="fill_parent"
    android:layout_height="0dip" 
    android:orientation="vertical" 
    android:gravity="center_horizontal"
        >

        <LinearLayout android:layout_height="0dp" android:layout_width="fill_parent"
            android:layout_weight="1"
            android:id="@+id/temp"></LinearLayout>
    <ImageButton
        android:id="@+id/btnLogin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/login"
        android:contentDescription="@string/Img"  
        android:layout_weight="1"
        />
    <LinearLayout android:layout_height="0dp" android:layout_width="fill_parent"
            android:layout_weight="1"></LinearLayout>
    <ImageButton
        android:id="@+id/btnFbLogin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
         android:background="@drawable/btnfb"
         android:contentDescription="@string/Img"
        />
    <LinearLayout android:layout_height="0dp" android:layout_width="fill_parent"
            android:layout_weight="1"></LinearLayout>
    <ImageButton
        android:id="@+id/btnRegister"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/btnregister"
        android:contentDescription="@string/Img"/>
</LinearLayout>
</LinearLayout>


提前致谢

【问题讨论】:

    标签: android android-linearlayout


    【解决方案1】:

    嵌套权重不利于性能,因为:

    布局权重需要对小部件进行两次测量。当一个权重非零的 LinearLayout 嵌套在另一个权重非零的 LinearLayout 中时,测量次数会呈指数增长。

    最好使用RelativeLayouts并根据其他视图的位置调整您的视图,而不使用特定的dpi值。

    礼貌:Why are nested weights bad for performance? Alternatives?

    【讨论】:

    • 为什么这被否决了?我需要一个理由。
    【解决方案2】:

    嵌套权重不利于性能,因为:

    布局权重需要对小部件进行两次测量。当一个 具有非零权重的 LinearLayout 嵌套在另一个内部 具有非零权重的 LinearLayout,然后是测量次数 成倍增长。

    最好使用RelativeLayouts 并根据其他视图的位置调整视图而不使用特定的dpi值。

    参考:http://developer.android.com/resources/articles/layout-tricks-efficiency.html

    在大多数情况下,您可以使用 RelativeLayout 来避免这种昂贵的测量。在 RelativeLayout 中,视图与其父视图、RelativeLayout 本身或其他视图对齐。

    为了清楚地了解视图是如何相互定位的,可以使用 Android SDK 的 HierarchyViewer 透视图来捕获布局的线框。

    【讨论】:

    • 链接已损坏 :(
    【解决方案3】:

    尝试从线性布局中删除android:weightSum="10",同时删除android:layout_weight="1"

    现在您看到警告已经消失了!

    确保在线性布局中保持按钮和其他内容的权重!

    我希望这可以解决你的问题:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多