gpt4 book ai didi

java - getHeight 为所有 Android UI 对象返回 0

转载 作者:IT老高 更新时间:2023-10-28 13:17:55 25 4
gpt4 key购买 nike

我正在构建一个 UI,它在 XML 中都是静态定义的。所有的东西都有重量,虽然它看起来不错,但我想看看这些东西实际上有正确的高度等等。问题是,无论我在哪里为格式布局调用 .getHeight(),我都得到了 0。我在 onCreate() 和 onStart() 中都进行了尝试。一样。也适用于所有 UI 对象。有什么想法吗?

package com.app.conekta;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.Toast;

public class Conekta extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

}

@Override
public void onStart() {

super.onStart();


}

@Override
public void onResume() {
super.onResume();

FrameLayout fl1 = (FrameLayout) findViewById(R.id.headerFrameLayout);
FrameLayout fl2 = (FrameLayout) findViewById(R.id.footerFrameLayout);
Button b=(Button) findViewById(R.id.searchButton);

Log.d("CONEKTA", String.valueOf(b.getHeight()));

}
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >



<FrameLayout
android:id="@+id/headerFrameLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.05"
android:background="#597eAA" >

<ImageView
android:id="@+id/logoImage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#7ba1d1"
android:src="@drawable/logo_conekta" />
</FrameLayout>

<LinearLayout
android:id="@+id/bodyLinearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:background="#f3f3f3"
android:orientation="horizontal" >

<FrameLayout
android:id="@+id/leftBlankFrameLayout"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="0.15"
android:background="#f3f3f3" >
</FrameLayout>

<LinearLayout
android:id="@+id/centerVerticalLayout"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="0.7"
android:orientation="vertical" >

<FrameLayout
android:id="@+id/topCenterFrameLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.35" >
</FrameLayout>

<TextView
android:id="@+id/venueLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.025"
android:text="What are you looking for?"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000" />

<EditText
android:id="@+id/venueTextField"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.025" >

<requestFocus />
</EditText>

<FrameLayout
android:id="@+id/middleCenterFrameLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.05" >
</FrameLayout>

<TextView
android:id="@+id/locationLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.025"
android:text="Where?"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000" />

<AutoCompleteTextView
android:id="@+id/locationTextField"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.025"
android:text="" />

<LinearLayout
android:id="@+id/buttonLinearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.05"
android:background="#f3f3f3"
android:orientation="horizontal" >

<FrameLayout
android:id="@+id/leftButtonLinearLayout"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="0.1" >
</FrameLayout>

<Button
android:id="@+id/searchButton"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="0.8"
android:background="#6fa8dc"
android:text="Search" />

<FrameLayout
android:id="@+id/rightButtonLinearLayout"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="0.1" >
</FrameLayout>
</LinearLayout>

<FrameLayout
android:id="@+id/bottomCenterFrameLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.35" >
</FrameLayout>
</LinearLayout>

<FrameLayout
android:id="@+id/rightBlankFrameLayout"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="0.15"
android:background="#f3f3f3" >
</FrameLayout>
</LinearLayout>

<FrameLayout
android:id="@+id/footerFrameLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.15"
android:background="#7ba1d1" >
</FrameLayout>

</LinearLayout>

最佳答案

它是 0,因为在 onCreate 和 onStart 中,实际上还没有绘制 View 。您可以通过监听实际绘制 View 的时间来解决此问题:

final TextView tv = (TextView)findViewById(R.id.venueLabel);
final ViewTreeObserver observer= tv.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
tv.getHeight()
observer.removeGlobalOnLayoutListener(this);
}
});

删除监听器的调用是为了防止在布局更改时重复调用您的自定义处理程序...如果您想获得这些,可以省略它。

关于java - getHeight 为所有 Android UI 对象返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8170915/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com