1

我是安卓新手。现在我收到嵌套重量性能不佳的警告。我的第一个屏幕中只有一个图像和三个按钮。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>


提前致谢

4

3 回答 3

6

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

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

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

礼貌:为什么嵌套权重不利于性能?备择方案?

于 2013-02-06T06:01:21.360 回答
0

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

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

最好使用RelativeLayout并根据其他视图的位置调整视图,而无需使用特定的 dpi 值。

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

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

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

于 2013-02-06T06:10:51.760 回答
0

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

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

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

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

于 2014-03-26T01:17:45.620 回答