java - LinearLayout提供的onMeasure()参数

标签 java android user-interface

我创建了一个自定义 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/

相关文章:

java - 抽象类和枚举

java - Oracle Coherence 通读配置

java - 如何修复解析错误(使用 LOOKAHEAD)

Android 库矢量转换为默认混淆的 png 和 Kotlin 文件

python - 基本的 wx 大小问题

java - 使用递归语法的自顶向下解析

android - 多个 Intent 过滤器

android - 数字不出现在 ADT 编辑器的 XML 文件中

javascript - 移动 Safari 上的可保持 html 按钮

java - 如何将控制台迁移到 Swing 应用程序