繁体   English   中英

替代使用LinearLayouts的嵌套权重

[英]Alternative to nested weights with LinearLayouts

我想实现以下目标:

在此输入图像描述

它适用于以下布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_weight="3"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <fragment 
            android:name="com.bobjohn.DetailsMenuFragment"
            android:id="@+id/detailsMenuFragment"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="6"
            />

        <fragment 
            android:name="com.bobjohn.SummaryFragment"
            android:id="@+id/summaryFragment"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_margin="10dp"
            android:layout_weight="4"/>
    </LinearLayout>

    <TextView 
        android:layout_width="0dp"
        android:layout_weight="7"
        android:layout_height="fill_parent"
        android:text="Test Text"/>
</LinearLayout>

但是,我收到有关嵌套权重对性能不利的警告。 我理解错误,但我不知道如何以另一种方式表达这种布局。 有什么选择?

SUPPORT Libs中有新的更新,请检查接受的答案。

更新答案: -

无论何时创建任何视图,它都会调用它的度量事件来了解屏幕上视图的高度宽度。如果您没有使用WRAP_CONTENT或FILL_PARENT或FIXEDSIZE并使用权重,那么在屏幕上呈现布局会变得更加复杂。

手段,

首先渲染主要布局并调用它的度量。然后根据权重,所有子视图都会递归调用它的度量事件,因此它会消耗更多的时间来加载。

因此, 应该避免重量嵌套

嵌套权重的替代方案: -

您应该考虑使用特定于不同大小的不同布局和可绘制文件夹。 使用特定的高度宽度在XML中编写视图或使其成为wrap_content并使用特定的背景图像或使其成为fill_parent。

我认为,作为开发人员,我们可能会错误几次,但作为创建者Android(Lint),他们可能只是在极少数情况下出错,应该听取这些警告以使您的代码更好。


以下答案是缺乏关于安卓布局的知识

AFAIK,我认为你做得对,这是同样最好的书面XML。 您已经完全使用了权重属性,因为它应该被使用。 你只需忽略警告。

有什么选择?

我已经在我的项目中以相同的方式编写了所有的XML,所以这是我的最佳选择,所以我认为除了使用RelativeLayout 作为父布局之外,还有其他任何替代CODE的XML来获得这样的布局。 一些固定大小的子视图的高度和宽度 我仍然建议你保持原样。


我会删除这个答案,因为我仍然不完全了解Android Layouts,但保留它以接收新的评论并基于此回答

是的,我们可以通过android的percent support library替代嵌套的LinearLayout weight

代码和概念在这里!

GitHub项目在这里!

考虑这个简单的布局,我完全避免了LinearLayout权重属性

百分比支持库

<android.support.percent.PercentRelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/fifty_huntv"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#ff7acfff"
        android:text="20% - 50%"
        android:textColor="@android:color/white"
        app:layout_heightPercent="20%"
        app:layout_widthPercent="50%" />
    <TextView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_toRightOf="@id/fifty_huntv"
        android:background="#ffff5566"
        android:text="80%-50%"
        app:layout_heightPercent="80%"
        app:layout_widthPercent="50%"
        />

</android.support.percent.PercentRelativeLayout>

非常棒 !!!

我想(而且我可能会因此受到抨击),但我认为我的手机还有一个四核处理器可以与大多数人家用PC竞争(如果不是完全毁掉的话)。

我也认为这种硬件功能是手机的未来。

所以我得出一个结论,只要你没有被嵌套(在MHO中,布局应该永远不会超过4级,如果是你可能做错了),你的手机可能会少关心关于有重量。

您可以做很多事情会对性能产生更深远的影响,然后担心您的处理器会做一些额外的数学运算。

(请注意,我有点幽默,所以不要在这篇文章中过于认真,除此之外,还有其他事情你应该先优化的想法,而担心2-3级深度的重量并没有帮助你的健康)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM