【问题标题】:Toolbar does not show up in Xamarin.Android工具栏未显示在 Xamarin.Android 中
【发布时间】:2017-01-09 15:21:28
【问题描述】:

我正在尝试用工具栏替换ActionBar,但工具栏没有显示。我不明白为什么我的页面没有工具栏仍然是空白的,我错过了什么?

我的风格:

  <style name="MyThemeActionBar" parent="Theme.AppCompat.Light">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>  
  </style>

我的toolbar.axml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:id="@+id/toolbar"
  android:layout_height="wrap_content"
  android:minHeight="?attr/actionBarSize"
  android:background="?attr/colorPrimary"
  android:layout_width="match_parent">

  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/toolbartitile"
    android:text="Kodej"/>

</android.support.v7.widget.Toolbar>

我的活动:

[Activity(Label = "MainLoginActivity" , Theme = "@style/MyThemeActionBar")]
public class MainLoginActivity : AppCompatActivity
{
    private ISharedPreferences sp;
    private Android.Support.V7.Widget.Toolbar toolbar;

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

        SetContentView(Resource.Layout.MainLogin);

        toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
        SetSupportActionBar(toolbar);

        sp = GetSharedPreferences(null, FileCreationMode.Private);

        TextView tv = FindViewById<TextView>(Resource.Id.emailMain);
        tv.Text = sp.GetString("email", null);

        tv.Click += (object sender, EventArgs e) =>
        {
            Android.Support.V7.App.AlertDialog.Builder ad = new Android.Support.V7.App.AlertDialog.Builder(this);

            ad.SetTitle("Logout");
            ad.SetMessage("Are you sure you want to logout");

            ad.SetPositiveButton("Yes", (senderAlert, args) => 
            {
                ISharedPreferencesEditor editor = sp.Edit();
                editor.Remove("email");
                editor.Apply();

                StartActivity(typeof(MainActivity));

                Finish();
            });

            ad.SetNegativeButton("Cancel", (senderAlert, args) => {});

            Dialog dialog = ad.Create();
            dialog.Show();
        };

    }
}

【问题讨论】:

    标签: android xamarin xamarin.android


    【解决方案1】:

    我认为问题可能出在代码上:

    toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
    

    由于您没有发布MainLogin 布局的代码,因此当您在MainLoginActivity 中编写代码时,上下文为MainLoginActivity,您应确保有一个Android.Support.V7.Widget.Toolbar,其ID 为@ 987654327@存在于MainLogin布局中,否则,这将返回一个null,并且当你SetSupportActionBar(toolbar);时它不会抛出异常。

    要解决此问题,您可以:

    1. Toolbar 的布局移动到MainLogin 布局中。

    2. 或者将 Toolbar 保留在工具栏布局中,但在 MainLogin 布局中使用它,例如:

    MainLogin 布局:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
      <include android:id="@+id/toolbar"
               layout="@layout/mytoolbar" />
    </LinearLayout>
    

    【讨论】:

    • 不幸的是我忘记了这篇文章但是你是对的错误是我忘了把它放在主登录但是谢谢你(你刚刚知道我可以使用 所以谢谢)
    • @jgauthier,那么如果这个答案有帮助,请mark this answer
    猜你喜欢
    • 1970-01-01
    • 2012-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-04
    • 2015-02-04
    • 1970-01-01
    相关资源
    最近更新 更多