Android布局设置layout_weight但是无效(比重、占比不起作用)

今天在写代码的时候,测试人员反馈一个bug,一个控件的高度失效了。视图层级类似于下面的代码,(问题代码)

<?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="wrap_content"
    android:orientation="vertical"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="30dp"></LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical"
        android:background="@android:color/holo_red_dark"
        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="vertical"
            android:background="@android:color/holo_blue_dark"
            >

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:background="@android:color/holo_green_dark"
            ></LinearLayout>
    </LinearLayout>



</LinearLayout>

真实境况是使用下面的方法加载的层级view

public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)

然后我试了一下,设置固定高度android:layout_height="100dp"是有效果的,那么就排除了调用

public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)

时设置root ==null的情况。
最后 终于找到了原因,原来是一时马虎,把根目录的
android:layout_height="wrap_content"改为android:layout_height="match_parent"

<?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="wrap_content"
    android:orientation="vertical"
    >

问题就解决了。 虽然这个问题没有多少原理性的知识,但是记录自己遇到的bug,以及解决bug,排查问题的思路。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容