android MessureSpec.Mode 什么时候是UNSPECIFIED?

问题描述:

以前累积的知识,都知道UNSPECIFIED这种模式很少出现,一般都是系统里面出现的。但是最近我自定义VIew的时候就遇到了,不知道如何解决.

 <?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:background="@color/colorPrimary"
    android:orientation="horizontal">


    <MyTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/colorAccent"
        android:gravity="center"
        android:text="英雄"
        android:textSize="20sp" />

    <MyTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/colorAccent"
        android:gravity="center"
        android:text="击杀"
        android:textSize="20sp" />

    <MyTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/colorAccent"
        android:gravity="center"
        android:text="阵亡"
        android:textSize="20sp" />

</LinearLayout>

这一个LinearLayout布局,我想在我的自定义布局中,放到指定的位置,然后通过

 LayoutInflater.from(this).inflate(R.layout.head,null)

来加载这个布局,赋值给这个变量View view,然后在调用addView(view),将这个布局加入到我的自定义View中去。

在我的自定义布局onMeasure()方法中,调用这个view.measure()来测量宽高,可是发现出来的效果并不是我想的那样,三个TextView并没有平分LinearLayout的宽度,然后调试了下发现,这些TextView的onMeasure方法中传入的MeasureSpec.Mode都是UNSPECIFIED,这是为啥啊?

不好意思,这个值我也不知道啥时候碰到了,改了下代码发现又没有了。。。。。应该是我当时写的时候哪里出了点问题,现在好了。

在你的自定义控件中用LayoutInflater.from(this).inflate(R.layout.head,this,false)用这个方法试试

你要看你下你的布局 你的LinearLayout 设置的是横向的 但是你的子布局全是充满屏幕 就这点感觉就均分不了的。