簡體   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