java gridlayout 权重_android - 为什么嵌套权重不利于性能? 备择方案?

这篇博客介绍了如何使用Android的百分比支持库替代传统的LinearLayout权重,以解决性能问题和减少嵌套带来的复杂性。通过示例展示了如何在XML中设置约束布局,利用`layout_constraintHeight_percent`和`layout_constraintWidth_percent`属性实现比例布局,从而提高布局效率。
摘要由CSDN通过智能技术生成

更新:我们知道支持库百分比已从API级别26弃用.LinearLayout是实现相同平面xml结构的新方法。

更新了Github项目

更新样本:

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">

android:id="@+id/fifty_thirty"

android:layout_width="0dp"

android:layout_height="0dp"

android:background="#ffff8800"

android:gravity="center"

android:text="@string/fifty_fifty_text"

android:textColor="@android:color/white"

app:layout_constraintHeight_default="percent"

app:layout_constraintHeight_percent="0.5"

android:textSize="25sp"

app:layout_constraintTop_toTopOf="parent"

app:layout_constraintWidth_default="percent"

app:layout_constraintWidth_percent="0.5" />

android:layout_width="0dp"

android:layout_height="0dp"

android:background="#ffff5566"

android:gravity="center"

android:text="@string/fifty_fifty_text"

android:textColor="@android:color/white"

android:textSize="25sp"

app:layout_constraintHeight_default="percent"

app:layout_constraintHeight_percent="0.5"

app:layout_constraintLeft_toRightOf="@id/fifty_thirty"

app:layout_constraintTop_toBottomOf="@id/fifty_thirty"

app:layout_constraintWidth_default="percent"

app:layout_constraintWidth_percent="0.5" />

更新:好消息android百分比支持库解决了我们的性能问题和嵌套凌乱加权LinearLayout

compile 'com.android.support:percent:23.0.0'

在这里演示

考虑这个简单的布局来演示相同的。

vUJNW.png

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">

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%" />

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%"

/>

避免性能降低嵌套LinearLayout与weights.Really真棒!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值