问题
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