无法在主活动中使用导航视图在导航抽屉中设置文本视图.Xamarin 安卓 [英] Can't set the textview in the navigation drawer using navigation view in main activity. Xamarin Android

查看:24
本文介绍了无法在主活动中使用导航视图在导航抽屉中设置文本视图.Xamarin 安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我设置所有图标、菜单、抽屉和导航视图的主要活动.在这里,我通过将 nav_header 膨胀到视图中并设置了 textview 来设置另一个视图,但我似乎仍然无法更改导航抽屉中的 textview.主活动

This is the main activity where I am setting all the icons, menu, drawer and my navigation view. Here I set another view by inflating nav_header into the view and set the textview but I still can't seem to change the textview in the navigation drawer. MainActivity

using Android.App;
using Android.Content.PM;
using Android.OS;
using Android.Support.V4.Widget;
using Android.Views;    
using RoutineApp.Fragments;
using Android.Support.V7.App;
using Android.Support.V4.View;
using Android.Support.Design.Widget;
using Android.Widget;
using Android.Content;

namespace RoutineApp
{
    [Activity(Label = "@string/app_name", MainLauncher = true, LaunchMode = LaunchMode.SingleTop)]
    public class MainActivity : AppCompatActivity
    {

        DrawerLayout drawerLayout;
        NavigationView navigationView;    
        IMenuItem previousItem;
        TextView UserNameTxt;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.main);
            var toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
            if (toolbar != null)
            {
                SetSupportActionBar(toolbar);
                SupportActionBar.SetDisplayHomeAsUpEnabled(true);
                SupportActionBar.SetHomeButtonEnabled(true);
            }    
            drawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);  
            SupportActionBar.SetHomeAsUpIndicator(Resource.Drawable.ic_menu);
            navigationView = FindViewById<NavigationView>(Resource.Id.nav_view);
            LayoutInflater inflater = (LayoutInflater)GetSystemService(Context.LayoutInflaterService);
            View view = inflater.Inflate(Resource.Layout.nav_header, null);
            UserNameTxt = view.FindViewById<TextView>(Resource.Id.UserNameTxt);
            UserNameTxt.Text = "Yousuf";
            navigationView.NavigationItemSelected += (sender, e) =>
            {
                if (previousItem != null)
                    previousItem.SetChecked(false);

                navigationView.SetCheckedItem(e.MenuItem.ItemId);    
                previousItem = e.MenuItem;    
                switch (e.MenuItem.ItemId)
                {
                    case Resource.Id.nav_home_1:
                        ListItemClicked(0);
                        break;

                    case Resource.Id.nav_home_2:
                        ListItemClicked(1);
                        break;
                }
                drawerLayout.CloseDrawers();
            };
            if (savedInstanceState == null)
            {
                navigationView.SetCheckedItem(Resource.Id.nav_home_1);
                ListItemClicked(0);
            }
        }
        int oldPosition = -1;
        private void ListItemClicked(int position)
        {
            if (position == oldPosition)
                return;
            oldPosition = position;
            Fragment fragment = null;
            switch (position)
            {
                case 0:
                    fragment = Fragment1.NewInstance();
                    break;
            }
            FragmentTransaction fragmentTx = this.FragmentManager.BeginTransaction();
            fragmentTx.Replace(Resource.Id.content_frame, fragment);
            fragmentTx.AddToBackStack(null);
            fragmentTx.Commit();
        }
        public override bool OnOptionsItemSelected(IMenuItem item)
        {
            switch (item.ItemId)
            {
                case Android.Resource.Id.Home:
                    drawerLayout.OpenDrawer(GravityCompat.Start);
                    return true;
            }
            return base.OnOptionsItemSelected(item);
        }
    }
}

这是主要的布局 axml,其中设置了导航和抽屉,在导航中调用标题.ma​​in.axml

This is the main layout axml where the navigation and drawer are set the header is called in the navigation. main.axml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<!-- The main content view -->

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <include
                android:id="@+id/toolbar"
                layout="@layout/toolbar"
                app:layout_scrollFlags="scroll|enterAlways" />

        </android.support.design.widget.AppBarLayout>

        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/toolbar_layout" />
    </RelativeLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/nav_menu" />

</android.support.v4.widget.DrawerLayout>

这是我在导航视图中调用的 nav_header,其中包含我试图在主要活动中更改的文本视图.nav_header.axml

This is my the nav_header which I have called in the navigation view, which contains the textview which I'm trying to change on main activity. nav_header.axml

    <TextView
        android:id="@+id/UserNameTxt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Username"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
</LinearLayout>

名称不变,请帮忙.

推荐答案

您使用 LayoutInflatornav_header 中扩展视图,这意味着您从nav_header.xml 并更改了它的子文本视图的文本,并没有在您的活动中使用这个新视图.因此,您原始活动的文本不会改变.

You use LayoutInflator to inflate a view from nav_header, which means you created a total new View from nav_header.xml and changed it's sub textview's text and didn't use this new view in your activity. Thus the text of your original activity won't change.

解决方案:像这样修改您的活动代码:

solution: Modify your activity codes like this:

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    SetContentView(Resource.Layout.main);
    var toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
    if (toolbar != null)
    {
        SetSupportActionBar(toolbar);
        SupportActionBar.SetDisplayHomeAsUpEnabled(true);
        SupportActionBar.SetHomeButtonEnabled(true);
    }    
    drawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);                SupportActionBar.SetHomeAsUpIndicator(Resource.Drawable.ic_menu);
    navigationView = FindViewById<NavigationView>(Resource.Id.nav_view);
    //LayoutInflater inflater = (LayoutInflater)GetSystemService(Context.LayoutInflaterService);
    //View view = inflater.Inflate(Resource.Layout.nav_header, null);
    //UserNameTxt = view.FindViewById<TextView>(Resource.Id.UserNameTxt);
    //UserNameTxt.Text = "Yousuf";
    LinearLayout header=(LinearLayout)navigationView.GetHeaderView(0);
    UserNameTxt = header.FindViewById<TextView>(Resource.Id.UserNameTxt);
    UserNameTxt.Text = "Yousuf";
    ...

这篇关于无法在主活动中使用导航视图在导航抽屉中设置文本视图.Xamarin 安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆