Android:ImageView、LinearLayout 和TextView 高度返回负值,0 和0,为什么?

标签 android android-layout

我试图在我的线性布局中缩放图像以填充可用空间,但我不明白我获得的布局宽度值。这是我的 main.xml 布局文件的相关部分:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LeftButtonsLayout"
    android:layout_width="0dip"
    android:layout_height="fill_parent"
    android:layout_weight="10"
    android:orientation="vertical" >

    <TextView
    android:id="@+id/Jump"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/jump"
    android:textStyle="bold"
    android:padding="5dip"
    />
    <ImageView
    android:id="@+id/JumpButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:src="@drawable/jump"
    android:contentDescription="@string/jump"
    android:padding="5dip"
    />
<LinearLayout

这是我的 Activity 的 onCreate() 方法,它有一个调试打印:

public void onCreate (Bundle savedInstanceState) {

    setContentView(R.layout.main);

    LinearLayout leftButtonsLayout = (LinearLayout) findViewById(R.id.LeftButtonsLayout);
    imageView = (ImageView) findViewById(R.id.ResetButton);

    Log.d("DEBUG", CLASS_NAME + "scaleLeftButtonsLayoutContents: \n" + 
          "linear layout height: " + leftButtonsLayout.getHeight() + "\n" +
      "text height: " + ((TextView)findViewById(R.id.Jump)).getHeight()  + "\n" +
          "image height: " + imageView.getLayoutParams().height); 

}

1) 如果我在 Log.d() 调试打印之后放置 setContentVew() 调用,我会得到空指针异常。为什么? LinearLayout 在 View 上使用之前是否未分配内存?

2) 我看到的指纹是: 线性布局高度:0 文字高度:0 图片高度:-2 我在这里做错了什么?我希望在这里看到合理的值,因为我可以在设备屏幕上看到 imageView。

3) 我打算使用以下方法缩放图像: imageView.getLayoutParams().height = newHeight.这样做对吗?这样做会自动更新屏幕上的 imageView,还是我必须再次执行 setContentView()?

预先感谢您的帮助。

更新

谢谢大家的回答。我已经覆盖了 Activity 的 onWindowFocusChanged() 方法,但是当我检查下面嵌套的 ImageView 的大小时,它被报告为 -2。调整它的大小是有效的,但我很好奇为什么它应该有一个理智的值时它是 -2。我的代码如下:

@Override
public void onWindowFocusChanged (boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);

    if (hasFocus)
        scaleLeftButtonsLayoutContents();
}


private void scaleLeftButtonsLayoutContents () {
    LinearLayout leftButtonsLayout = (LinearLayout)findViewById(R.id.LeftButtonsLayout);

    imageView = (ImageView) findViewById(R.id.JumpButton);      
    Log.d("TAG", CLASS_NAME + "JumpButton.height " + imageView.getLayoutParams().height);
    imageView.getLayoutParams().height = verticalSpaceAvailable;
    imageView.getLayoutParams().width = verticalSpaceAvailable;

    leftButtonsLayout.requestLayout();
}

这会产生打印: JumpButton.height -2

调整大小生成了一个正常的图像,但为什么初始高度为 -2?

最佳答案

为了回答你的观点,

1) 这是因为您还没有初始化您的 Button 或 ImageView。由于您在执行此操作之前调用了 Log,显然 Button 和 ImageView 为空,因此您得到了异常。

2)并且初始化并不意味着你的 View 被完全绘制出来为你提供宽度和高度。所以你必须提供时间来吸引自己。但不幸的是,我们不知道绘制所需的确切时间。所以Android提供了这个方法,

  @Override 
public void onWindowFocusChanged(boolean hasFocus) 
{ 
   // which gets called when your view is drawn. 
 }  

刚才回答了一个类似的问题here .

所以你要做的是,在你的 Activity 中的这个方法中添加你的日志,然后检查生成的宽度和高度。

3) 要回答你的第三个问题,你绝对不应该再次调用 setContentView() ,这可能会抛出一些其他异常。但是在考虑缩放时,您可能会使用一些位图来执行此操作。

关于Android:ImageView、LinearLayout 和TextView 高度返回负值,0 和0,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11078936/

相关文章:

java - 当我旋转 Android 设备时,XML 变回 main.xml

android - onPause/onResume 后无法绘制

android - 如何在样式文件中定义可绘制对象的大小?

android - 为 Android 按钮属性分配负值

android - OnItemSelected 不适用于自定义微调器适配器类

android - 如何在android中用一条线分隔布局的页脚

java - icu4j 太大了,还有其他选择吗?

android - 谷歌 Espresso 问题跟踪器问题去了哪里?

android - Firebase Firestore - 或查询

android - 在 View 底部放置一个线性布局