gpt4 book ai didi

android - 如何使用 Xamarin/Monodroid 设置自定义标题

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:52:56 28 4
gpt4 key购买 nike

有没有人使用 MonoDroid 成功设置自定义标题?

此代码失败:

base.OnCreate (bundle);     
SetContentView (Resource.Layout.WordSearchLayout);

//Customise the title bar
if (this.Window.RequestFeature(WindowFeatures.CustomTitle))
{
this.Window.SetFeatureInt (WindowFeatures.CustomTitle, Resource.Layout.custom_title);
}

奇怪的是,它在调用 RequestFeature 时失败,异常是 requestFeature() must be called!

I/MonoDroid(26052): UNHANDLED EXCEPTION: Android.Util.AndroidRuntimeException: Exception of type 'Android.Util.AndroidRuntimeException' was thrown.
I/MonoDroid(26052): at Android.Runtime.JNIEnv.CallBooleanMethod (intptr,intptr,Android.Runtime.JValue[]) <0x0007c>
I/MonoDroid(26052): at Android.Views.Window.RequestFeature (Android.Views.WindowFeatures) <0x00157>
I/MonoDroid(26052): at com.xxx.android.WordSearchActivity.OnCreate (Android.OS.Bundle) <0x0013b>
I/MonoDroid(26052): at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) <0x0005f>
I/MonoDroid(26052): at (wrapper dynamic-method) object.1a40b50f-d523-4f52-b291-bcedd3a88965 (intptr,intptr,intptr) <0x00033>
I/MonoDroid(26052):
I/MonoDroid(26052): --- End of managed exception stack trace ---
I/MonoDroid(26052): android.util.AndroidRuntimeException: requestFeature() must be called before adding content
I/MonoDroid(26052): at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:181)
I/MonoDroid(26052): at com.xxx.android.WordSearchActivity.n_onCreate(Native Method)
I/MonoDroid(26052): at com.xxx.android.WordSearchActivity.onCreate(WordSearchActivity.java:30)
I/MonoDroid(26052): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1065)
I/MonoDroid(26052): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2745)
I/MonoDroid(26052): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2797)
I/MonoDroid(26052): at android.app.ActivityThread.access$2300(ActivityThread.java:135)
I/MonoDroid(26052): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2132)
I/MonoDroid(26052): at android.os.Handler.dispatchMessage(Handler.java:99)
I/MonoDroid(26052): at android.os.Looper.loop(Looper.java:143)
I/MonoDroid(26052): at android.app.ActivityThread.main(ActivityThread.java:4914)
I/MonoDroid(26052): at java.lang.reflect.Method.invokeNative(Native Method)
I/MonoDroid(26052): at java.lang.reflect.Method.invoke(Method.java:521)
I/MonoDroid(26052): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
I/MonoDroid(26052): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
I/MonoDroid(26052): at dalvik.system.NativeStart.main(Native Method)
E/mono (26052):
E/mono (26052): Unhandled Exception: Android.Util.AndroidRuntimeException: Exception of type 'Android.Util.AndroidRuntimeException' was thrown.
E/mono (26052): at Android.Runtime.JNIEnv.CallBooleanMethod (IntPtr jobject, IntPtr jmethod, Android.Runtime.JValue[] parms) [0x00000] in <filename unknown>:0
E/mono (26052): at Android.Views.Window.RequestFeature (WindowFeatures featureId) [0x00000] in <filename unknown>:0
E/mono (26052): at com.xxx.android.WordSearchActivity.OnCreate (Android.OS.Bundle bundle) [0x00000] in <filename unknown>:0
E/mono (26052): at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (IntPtr jnienv, IntPtr native__this, IntPtr native_savedInstanceState) [0x00000] in <filename unknown>:0
E/mono (26052): at (wrapper dynamic-method) object:1a40b50f-d523-4f52-b291-bcedd3a88965 (intptr,intptr,intptr)
E/mono (26052): --- End of managed exception stack trace ---
E/mono (26052): android.util.AndroidRuntimeException: requestFeature() must be called before adding content
E/mono (26052): at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:181)
E/mono (26052): at com.xxx.andro

更改调用 SetContentView 的代码顺序(见下文)有一些效果:

 base.OnCreate (bundle);        

//Customise the title bar
if (this.Window.RequestFeature (WindowFeatures.CustomTitle))
{
this.Window.SetFeatureInt (WindowFeatures.CustomTitle, Resource.Layout.custom_title);
}
SetContentView (Resource.Layout.WordSearchLayout);

这将导致标题中出现一个空的灰色条,但没有其他内容。 custom_title 定义为:

<TextView 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myTitle"
android:text="This is my new title"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:textColor="@android:color/black"
android:background="@android:color/white"
android:layout_marginLeft="25px"
android:paddingTop="3px"
/>

模拟器和设备上的结果相同(灰色条)。

有什么想法吗?

最佳答案

在我的应用程序中,我使用此代码来设置自定义标题。

base.OnCreate (bundle);        
RequestWindowFeature(WindowFeatures.CustomTitle);
SetContentView (Resource.Layout.WordSearchLayout);
Window.SetFeatureInt (WindowFeatures.CustomTitle, Resource.Layout.custom_title);

关于android - 如何使用 Xamarin/Monodroid 设置自定义标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8264077/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com