【发布时间】:2014-12-17 17:06:48
【问题描述】:
我想要在Activity 布局的顶部有一个SearchView,然后在其下方我需要添加两个水平对齐的fragment,这样右边的水平方向是水平方向的两倍比左边的空间。
所以我的意思是:
包含两个片段的ViewGroup 应在绘制SearchView 后占用所有剩余空间。所以我给了它一个layout_weight 的1。
然后我为碎片网添加了一个LinearLayout和两个FrameLayouts,并给左边一个1的Layout_weight,右边一个2的值。
但是我收到了 Lint 警告说嵌套权重不利于性能。
我在 SO 和 found this 上读到了它:
布局权重需要对小部件进行两次测量。
basic tutorial on the developer training here 是这样说的:
为了在指定权重时提高布局效率,您 应将 EditText 的宽度更改为零 (0dp)。设置 宽度为零提高了布局性能,因为使用“wrap_content” 因为宽度要求系统计算宽度为 最终不相关,因为重量值需要另一个宽度 计算以填充剩余空间。
但是由于在我的布局中,当父 LinearLayout 的 orientation 是 vertical 时,我设置了 layout_width="0dp",而当父 LinearLayout 的 orientation 是 horizontal 时,layout_height="0dp" .为什么我仍然收到 Lint 警告?
对此我能做些什么?因为RelativeLayout 无法指定元素要占用多少“剩余空间”(由layout_weight 指定,RelativeLayout.LayoutParams 中似乎没有包含)。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<android.support.v7.widget.SearchView
android:id="@+id/searchActivity_searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:orientation="horizontal"
android:layout_weight="1"
android:layout_height="0dp"
android:layout_width="match_parent" >
<FrameLayout
android:id="@+id/searchActivity_frameLayout"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" /><!-- Nested weights are bad for performance Lint Warning -->
<FrameLayout
android:id="@+id/searchActivity_frameLayout1"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
【问题讨论】:
标签: android android-layout android-linearlayout android-relativelayout android-layout-weight