我创建了一个自定义 View ,并尝试实现 onMeasure() 以在所有情况下都能正常工作。我目前想知道当我的 View 放置在这样的线性布局中时作为 width/heightMeasurementSpec 传递的参数:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<MyView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/someId"></MyView>
</LinearLayout>
在这种情况下做完
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
int height = MeasureSpec.getSize(heightMeasureSpec);
int heightmode = MeasureSpec.getMode(heightMeasureSpec);
//...
}
我有 height = 271(在 480x320 屏幕上)和 heightmode = AT_MOST,这似乎是有道理的。
但是当我在 View 后添加一些像这样的 TextViews 时:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<MyView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/someId"></MyView>
<TextView android:text="1" android:layout_width="fill_parent"
android:layout_height="wrap_content"></TextView>
<TextView android:text="2" android:layout_width="fill_parent"
android:layout_height="wrap_content"></TextView>
</LinearLayout>
我仍然得到相同的高度/高度模式值(实际上调用了两次 onMeasure,但参数相同)。我认为由于 TextViews 占用了一些空间,所以 MyView 的空间应该更少? (可以,但我需要知道我可以占用多少空间...)
有什么想法吗?非常感谢!
最佳答案
您在自定义 View 之后添加 TextView,因此它们的高度不会影响您的自定义 View 的高度。
关于java - LinearLayout提供的onMeasure()参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4704205/