2017-11-29 4 views
0

Android 용 사용자 정의 도구 모음을 사용하는 Xamarin Forms 프로젝트가 있습니다. 툴바는 Axml의 Layout으로 정의되어 있으며 툴바의 minHeight를 표준 액션 바 높이까지 설정합니다.내비게이션 탐색/동작 바 높이

내 응용 프로그램의 특정 페이지에 대해 위쪽 (탐색/동작) 막대의 높이가 필요한 사용자 지정 페이지 렌더러가 있습니다.

필자는 이미 FindViewByID로 높이를 얻고 TypedValue로 높이를 얻으려고했지만 그만 다시 0을 보냈습니다.

내 페이지 렌더러에서 막대의 높이를 높이려면 무엇을 시도해야합니까?

답변

1

App.cs (공통 코드)

public static int ActionBarHeight; 

을 얻을 것이다 MainActivity, OnCreate 메서드 (Droid 파트) :

Android.Content.Res.TypedArray styledAttributes = Theme.ObtainStyledAttributes(new int[] { Android.Resource.Attribute.ActionBarSize }); 
App.ActionBarHeight = (int)(styledAttributes.GetDimension(0, 0)/Resources.DisplayMetrics.Density); 
styledAttributes.Recycle(); 

높이를 픽셀 단위로 표시하려면 다음을 입력하십시오.

App.ActionBarHeight = (int)styledAttributes.GetDimension(0, 0); 
0

Xamarin Android에서는 OnMeasure 메서드가 호출 된 후에야 뷰의 너비와 높이를 얻을 수 있습니다. OnLayout 메서드는 OnMeasure 뒤에 호출됩니다. 따라서 너비와 높이를 OnLayout 방법으로 얻을 수 있습니다.

class MyRender: PageRenderer 
{ 
    protected override void OnElementChanged(ElementChangedEventArgs<Page> e) 
    { 
     base.OnElementChanged(e); 
    } 

    protected override void OnLayout(bool changed, int l, int t, int r, int b) 
    { 
     System.Diagnostics.Debug.Write("OnLayout start"); 
     base.OnLayout(changed, l, t, r, b); 
     for (int i = 0; i < ChildCount; i++) 
     { 
      Android.Views.View view = GetChildAt(i); 
      int height = view.Height; 
      int width = view.Width; 
      System.Diagnostics.Debug.Write("height=" + height); 
      System.Diagnostics.Debug.Write("width=" + width); 
     } 
     System.Diagnostics.Debug.Write("OnLayout end"); 

    } 

} 

당신은 폭과 높이를 얻기 전에 OnMeasure가 호출되어 있는지 확인해야합니다, 또는 당신이 항상 0