Ale*_*x B 64 java android android-layout android-framelayout
我正在构建一个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()));
}
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
Chr*_*ris 83
它是0,因为在onCreate和onStart中,视图实际上还没有被绘制.您可以通过侦听实际绘制视图来解决此问题:
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);
}
});
Run Code Online (Sandbox Code Playgroud)
删除侦听器的调用是为了防止在布局更改时重复调用自定义处理程序...如果要获取它们,可以省略它.
Dee*_*eeV 40
简而言之,onCreate(),onStart()或onResume()中尚未构建视图.由于它们在技术上不存在(就ViewGroup而言),它们的维度为0.
很长一段时间,你可以到这里更好地解释如何处理它.
小智 13
使用此功能可获得视图的高度或宽度
private int getHeightOfView(View contentview) {
contentview.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
//contentview.getMeasuredWidth();
return contentview.getMeasuredHeight();
}
Run Code Online (Sandbox Code Playgroud)
我再次回答这个问题,以确保像我这样的人在实施之前完全了解它是如何工作的。
问:为什么我的视图高度为 0?
A.因为您尝试获取的视图高度尚未构建在 onCreate()、onStart() 或 onResume() 中。
问:现在我应该在哪里以及如何获得视图的高度?
A.你仍然可以在任何你想要的地方获取视图的参数,onCreate()、onStart() 或 onResume()。像下面这样:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = (TextView)findViewById(R.id.textview1);
ViewTreeObserver viewTreeObserver = tv.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
viewHeight = tv.getHeight();
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
但是,这次您将等到获得视图的高度后再移除观察者。
tv.getViewTreeObserver().removeOnGlobalLayoutListener(this);
Run Code Online (Sandbox Code Playgroud)
问:那么在删除这个监听器之前我应该检查什么?
**A.**在删除 treeObserver 之前添加一个 if 条件,例如,
if (viewHeight != 0)
tv.getViewTreeObserver().removeOnGlobalLayoutListener(this);
Run Code Online (Sandbox Code Playgroud)
最终代码:
ViewTreeObserver viewTreeObserver = tv.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int viewHeight = tv.getHeight();
if (viewHeight != 0)
tv.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
}
Run Code Online (Sandbox Code Playgroud)
希望这能解释为什么 getHeight 返回 0 以及如何正确获取它。谢谢。