getHeight는 모든 Android UI 개체에 대해 0을 반환합니다.
저는 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>
요컨대, 뷰는 아직 onCreate (), onStart () 또는 onResume ()에서 빌드되지 않았습니다. 기술적으로 존재하지 않기 때문에 (ViewGroup에 관한 한) 차원은 0입니다.
오랫동안 여기로 이동하여 처리 방법에 대한 더 나은 설명을 얻을 수 있습니다.
It's 0 because in both onCreate and onStart, the view hasn't actually been drawn yet. You can get around this by listening for when the view is actually drawn:
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);
}
});
The call to remove the listener is there to prevent repeated invocations of your custom handler on layout changes... if you want to get those, you can omit it.
Use this function to get Height or Width of View
private int getHeightOfView(View contentview) {
contentview.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
//contentview.getMeasuredWidth();
return contentview.getMeasuredHeight();
}
I am answering this again to make sure people like me will understand how it works completely before implementing.
Q. Why do I get my view's height as 0?
A. Because the view's height that you are trying to get is not yet built in onCreate(), onStart(), or onResume().
Q. Now where and how am I supposed to get the view's height?
A. You can still get the view's params wherever you want it like, onCreate(), onStart(), or onResume(). Like below:
@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();
}
});
}
}
However this time you will wait until you get the view's height before removing observer.
tv.getViewTreeObserver().removeOnGlobalLayoutListener(this);
Q. So what should I check before removing this listener?
**A.**Just put a if condition before removing treeObserver like,
if (viewHeight != 0)
tv.getViewTreeObserver().removeOnGlobalLayoutListener(this);
Final code:
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);
}
});
}
Hope this explains why the getHeight returns 0 and how to get it properly. Thanks.
By getting your button in a View you can easily fetch its height etc. You can define an onClickListener
like so:
Button b=(Button) findViewById(R.id.searchButton);
b.setOnClickListener(ButtonClick);
android.view.View.OnClickListener ButtonClick= new android.view.View.OnClickListener() {
public void onClick(View v) {
v.getHeight();
}};
Hope this helps.
참고URL : https://stackoverflow.com/questions/8170915/getheight-returns-0-for-all-android-ui-objects
'developer tip' 카테고리의 다른 글
SpringJUnit4ClassRunner에 대한 log4j.properties를 구성하는 방법은 무엇입니까? (0) | 2020.11.20 |
---|---|
SimpleXML 개체를 배열로 변환 (0) | 2020.11.20 |
큰 파일의 줄 수 (0) | 2020.11.20 |
자바 스크립트를 사용하여 로케일 간단한 날짜 형식 가져 오기 (0) | 2020.11.19 |
Visual C # 2010 Express에서 디버그와 릴리스간에 전환하는 방법은 무엇입니까? (0) | 2020.11.19 |