如何动态设置Xamarin Android对话框活动的标题?

问题描述:

我在Xamarin Android上有2项活动。请注意,第二个样式是作为对话框打开的。当第二个活动打开我希望能够动态地设置标题对话框上如何动态设置Xamarin Android对话框活动的标题?

var activity = new Intent(this, typeof(ActivityTwo)); 
StartActivity(activity); 

[Activity(Label = nameof(ActivityOne), HardwareAccelerated = true, ScreenOrientation = Android.Content.PM.ScreenOrientation.Portrait)] 
public class ActivityOne : Activity { ... } 

[Activity(Label = nameof(ActivityTwo), HardwareAccelerated = true, ScreenOrientation = Android.Content.PM.ScreenOrientation.Portrait, Theme = "@android:style/Theme.Holo.Dialog")] 
public class ActivityTwo : Activity { ... } 

第一个调用第二个这样的。

我试图通过在第二活动的创作这样做:

protected override void OnCreate(Bundle bundle) 
{ 
    base.OnCreate(bundle); 
    SetContentView(Resource.Layout.ActivityTwo); 
    SetTitle(Resource.Id.title); 

    ... 
} 

其中标题定义为ActivityTwo.axml一个“隐藏”的TextView:

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/title" 
    android:text="Example Title" 
    android:visibility="gone" /> 

但是,这就是我获取运行应用程序时的对话标题:

dialog activity title

也没有SetTitle行,标题只是“ActivityTwo”。任何人都可以帮我弄清楚这个问题吗?

解决方法是使用在OnCreate中()

this.Title = "ExampleTitle"; 
+0

肯定!我错过了那一个。好 – apineda

如果您只想设置标题,则活动顶部的属性为标题。这就是为什么如果你不SetTitle它显示ActivityTwo因为你告诉它使用nameof(ActivityTwo)这样做。

[Activity(Label = "Here the title you want to show", HardwareAccelerated = true, ScreenOrientation = Android.Content.PM.ScreenOrientation.Portrait, Theme = "@android:style/Theme.Holo.Dialog")] 
public class ActivityTwo : Activity { ... } 

关于你在尝试什么,你应该传递一个Resource.String到那个方法。比方说,你在string.xml文件HelloActivityTitle

SetTitle(Resource.String.HelloActivityTitle); 

这也将设置标题条目。

第三种选择是接收一个string或Java的CharSequence”的Window.SetTitle

Window.SetTitle("Here the title you want to show"); 

UPDATE

Window.SetTitle("Here the title you want to show"); 

应从OnResume方法被调用。

但使用下面的方法从调用它210

SetTitle(Resource.String.HelloActivityTitle); 

清理并重建您的解决方案,以防万一。

希望它helps.-

+0

感谢以下!我忘记写的一件事是我想动态地设置标题,并且以上解决方案都不适用于我。你能否更新你的答案来解决这个问题?我也在更新这个问题。 – zalenix