2017-11-29 11 views
0

私はAndroid用のカスタムツールバーを使用するXamarinフォームプロジェクトを持っています。 ツールバーはAxmlのレイアウトとして定義されており、ツールバーのminHeightを標準のアクションバーの高さに設定します。ナビゲーション/アクションバーの高さを取得

私のアプリの特定のページについては、トップページ(ナビゲーション/アクション)バーの高さが必要なカスタムページレンダラーがあります。

私はすでにFindViewByIDで高さを取得しようとしていて、TypedValueで高さを取得しようとしましたが、それは0を返すだけでした。

私のページレンダラーでバーの高さを取得しようとするとほかに何ができますか?

答えて

1

App.cs(共通コード)

public static int ActionBarHeight; 

を取得することを確認する必要がありますMainActivity、OnCreateメソッド(ドロイド部分):

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