я пытаюсь получить высоту точки зрения, и он продолжает возвращаться 0.андроид: получить viewHeight программно
это то, что я пробовал:
View hourView = View.inflate(mContext, R.layout.calendar_hour_item,null);
hourViewHeight = hourView.findViewById(R.id.mainLayout).getHeight();
я также пытался hourViewHeight = hourView.getHeight();
как хорошо, но нет радость.
я называю эти две строки внутри метода Initialize(), расположенный здесь:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calendar_day_screen);
Intent intent = getIntent();
mDate = new DateTime(intent.getStringExtra("date"));
circleIcon = (ImageView) findViewById(R.id.circle);
mHoursLayout = (RelativeLayout) findViewById(R.id.dayLayout);
TopBarUtil topBar = new TopBarUtil(getApplicationContext());
circleIcon.setOnClickListener(topBar.onActionClickListener());
mContext = getApplicationContext();
initialize();
}
вот мой XML макет:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/mainLayout" android:background="@layout/border">
<TextView android:text="12am" android:id="@+id/hourTextView"
android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</RelativeLayout>
я basicllay хочу, чтобы получить высоту выше макета XML но он всегда возвращает 0.
редактирование: я также пробовал сделать это ниже:
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
final View hourView = View.inflate(mContext, R.layout.calendar_hour_item,null);
ViewTreeObserver observer = hourView.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
hourView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
hourViewHeight = hourView.getHeight();
Log.d(TAG, "hourViewHeight = " + hourViewHeight);
}
});
// hourViewHeight = hourView.findViewById(R.id.mainLayout).getHeight();
// Log.d(TAG, "hourViewHeight = " + hourViewHeight);
}
спасибо. вы были на месте. – jonney