首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >layout_width和layout_weight -性能

layout_width和layout_weight -性能
EN

Stack Overflow用户
提问于 2013-04-02 14:18:21
回答 2查看 2.5K关注 0票数 0
代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="60dip"
    android:orientation="horizontal"
    android:background="#80939393"
    android:gravity="center_vertical"
     >

    <CheckBox
        android:id="@+id/column1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0" />


    <View android:layout_height="fill_parent"
    android:layout_width="1dp"
    android:background="#90909090"/>


    <TextView
        android:id="@+id/column2"
        android:layout_width="30dip"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:gravity="center"
        android:text="@string/column_title_2" />

    <View android:layout_height="fill_parent"
    android:layout_width="1dp"
    android:background="#90909090"/>

    <LinearLayout
        android:layout_width="70dip"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:layout_weight="1"
        android:clickable="true"
        android:gravity="center_vertical"
        android:id="@+id/column3_container" >
        <TextView
            android:id="@+id/column3"
            android:layout_width="50dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="left"
            android:paddingLeft="8dp"
            android:text="@string/column_title_3" />
        <TextView
            android:id="@+id/sort_column_3"
            android:layout_width="20dip"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:gravity="center"
            android:textSize="24dp"
            android:paddingRight="8dp"
            android:text="@string/solid_up" />
    </LinearLayout>


    <View android:layout_height="fill_parent"
    android:layout_width="1dp"
    android:background="#90909090"/>


    <LinearLayout
        android:layout_width="70dip"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:layout_weight="1"
        android:clickable="true"
        android:gravity="center_vertical"
        android:id="@+id/column4_container" >
        <TextView
            android:id="@+id/column4"
            android:layout_width="50dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="left"
            android:paddingLeft="8dp"
            android:text="@string/column_title_4" />
        <TextView
            android:id="@+id/sort_column_4"
            android:layout_width="20dip"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:gravity="center"
            android:textSize="24dp"
            android:paddingRight="8dp"
            android:text="" />
    </LinearLayout>


    <View android:layout_height="fill_parent"
    android:layout_width="1dp"
    android:background="#90909090"/>


    <LinearLayout
        android:layout_width="70dip"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:layout_weight="1"
        android:clickable="true"
        android:gravity="center_vertical"
        android:id="@+id/column5_container" >
        <TextView
            android:id="@+id/column5"
            android:layout_width="50dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="left"
            android:paddingLeft="8dp"
            android:text="@string/column_title_5" />
        <TextView
            android:id="@+id/sort_column_5"
            android:layout_width="20dip"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:gravity="center"
            android:textSize="24dp"
            android:paddingRight="8dp"
            android:text="" />
    </LinearLayout>


    <View android:layout_height="fill_parent"
    android:layout_width="1dp"
    android:background="#90909090"/>


    <TextView
        android:id="@+id/column6"
        android:layout_width="50dip"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:gravity="center"
        android:text="@string/column_title_6" />

</LinearLayout>
    <View android:layout_height="1dp"
    android:layout_width="fill_parent"
    android:background="#90909090"/>
    <ListView
        android:id="@+id/mylist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:choiceMode="multipleChoice">
    </ListView>
    <View android:layout_height="1dp"
    android:layout_width="fill_parent"
    android:background="#90909090"/>

</LinearLayout>

在我的eclipse中,我收到了两个关于性能的警告。

Nested weights are bad for performance使用android:layout_weight="1" Use a layout_width of 0dip instead of 50dip for better performance

有人能说我为什么会犯这个错误吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-04-02 14:23:13

我的理解是:因为您使用的是layout_weight,所以它取代了layout_width。因此,从本质上说,layout_width被忽略了。但从技术上讲,它并没有被忽视,相反,系统正在应用layout_width="50dp",然后返回并进行数学运算来应用layout_weight="1",因此它所做的工作量是原来的两倍。它告诉您使用0dp,以避免做(不必要的)工作。

编辑

第二个问题Nested weights are bad for performance是因为您在LinearLayout上有一个权重,在这个LinearLayout中有一个权重,因此它们是“嵌套的”。警告告诉您,以这种方式设置布局可能不利于性能。理想的解决方法可能是想出一些方法来实现您想要的样子,使用RelativeLayout,而根本不使用权重。但在现实中,除非你特别注意到或听到业绩不佳的报告,否则你很可能会放弃它。

票数 2
EN

Stack Overflow用户

发布于 2013-04-02 14:22:04

使用weight属性时,它将根据weight的值计算该视图的宽度。如果不将视图的宽度设置为0dp,则会再次计算宽度,这是不必要的。因此,如果使用weight,然后宽度为0dp,它只需计算一次大小。

用于嵌套权重警告。

代码语言:javascript
复制
android:layout_width="70dip"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="1"

在这里删除这个线性布局中的权重属性

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15766488

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档