How to display a View in Full Size on screen in Xamarin Android?

不问归期 提交于 2020-07-22 18:07:39

问题


I am unable to set the View Size matching the Display Size, below is the code, it is a Transparent View

 TestView testView;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        testView = new TestView(this);
        SetContentView(testView);

    }

The Default Height of the View when the app runs is height=1206 but the Display Height is 1280, i have used the following methods to set the View Size match the Screen Size, but none works

Does not work

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);

    testView = new TestView(this);
    IWindowManager window = null;
        window = this.Application.GetSystemService(Context.WindowService).JavaCast<IWindowManager>();
        Display defaultDisplay = window.DefaultDisplay;
        DisplayMetrics display = new DisplayMetrics();
        defaultDisplay.GetRealMetrics(display);
        int width = display.WidthPixels;
        int height = display.HeightPixels;
        RelativeLayout.LayoutParams parms = new 
                       RelativeLayout.LayoutParams(width, height);
        testView.LayoutParameters = parms;
    SetContentView(testView);

}

Result:

Doesn not work:

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);

    testView = new TestView(this);
    SetContentView(testView);
    if (Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat)
        {

            Window.SetFlags(WindowManagerFlags.LayoutNoLimits,
                              WindowManagerFlags.LayoutNoLimits);
        }

}

<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowTranslucentStatus">true</item>

Result:

How to set the Display height to the View?


回答1:


You need to declare it in your manifest file that your activity should be of full screen:

<activity android:name=".ActivityName"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>

And if you want to get it through code just use these lines in your activity's onCreate method:

requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

Or Add the below line to the style.xml file

 //your theme
 <style name="Theme.AppCompat.Transparent.NoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
      //XML lines for full screen
      <item name="android:windowNoTitle">true</item>
      <item name="android:windowFullscreen">true</item>
 </style>

For more help take a look at this answer: Fullscreen Activity in Android?



来源:https://stackoverflow.com/questions/53337682/how-to-display-a-view-in-full-size-on-screen-in-xamarin-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!