以前累积的知识,都知道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,这是为啥啊?