如何在FrameLayout中获取NavigationDrawer的按钮

时间:2017-04-06 07:14:28

标签: xamarin xamarin.android

我是NavigationDrawer的新手,下面的代码正在运行。

但是如何获取homeLayout.axml中的按钮,该按钮在HomeFragment.cs中以下代码之后显示为膨胀。

   ft.Add(Resource.Id.HomeFrameLayout, new HomeFragment());
   ft.Commit();

1)我需要将eventHandler添加到此btn:homeLayout.axml中的btnProducts
- 在哪里添加下面的代码
 我需要添加到设置代码以获取btnProducts并为此按钮添加事件?

  SetContentView (Resource.Layout.????);
  Btn = FindViewById<Button>(Resource.Id.BtnGM);
  Btn.Click += Btn_Click1;

--- UI:

主布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minWidth="25px"
    android:minHeight="25px"
    android:fitsSystemWindows="true">
  <android.support.v4.widget.DrawerLayout
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:minWidth="25px"
      android:minHeight="25px"
      android:id="@+id/drawer_layout">
   <LinearLayout
      android:id="@+id/layout_main"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical">
   <include
      layout="@layout/app_bar" />
   <FrameLayout
     android:id="@+id/HomeFrameLayout"
     android:minWidth="25px"
     android:minHeight="25px"
     android:layout_width="match_parent"
     android:layout_height="match_parent" />
</LinearLayout>
<android.support.design.widget.NavigationView
   android:id="@+id/nav_view"
   android:layout_width="wrap_content"
   android:layout_height="match_parent"
   android:layout_gravity="start"
   app:menu="@menu/navmenu"
   app:headerLayout="@layout/headerdrawerlayout" /> </android.support.v4.widget.DrawerLayout>
</LinearLayout>

- 主UI代码

public class NaviDrawerActivity : AppCompatActivity
 {

    private SupportToolbar toolbar;
    DrawerLayout drawerLayout;     

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

      SetContentView(Resource.Layout.NaviDrawer);          
      drawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);

      //--- Init toolbar
      toolbar = FindViewById<SupportToolbar>(Resource.Id.app_bar);

      SetSupportActionBar(toolbar);
      SupportActionBar.SetTitle(Resource.String.app_name);
      SupportActionBar.SetDisplayHomeAsUpEnabled(true);
      SupportActionBar.SetDisplayShowHomeEnabled(true);

     //--- Attach item selected handler to navigation view

     var navigationView = FindViewById<NavigationView>(Resource.Id.nav_view);
     navigationView.NavigationItemSelected += NavigationView_NavigationItemSelected;

     //-- Create ActionBarDrawerToggle button and add it to the toolbar

    var drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, Resource.String.open_drawer, Resource.String.close_drawer);
    drawerLayout.SetDrawerListener(drawerToggle);
    drawerToggle.SyncState();

     //--load default home screen

      var ft = FragmentManager.BeginTransaction();
       ft.AddToBackStack(null);

       ft.Add(Resource.Id.HomeFrameLayout, new HomeFragment());
       ft.Commit();
   }


  //---define custom title text

  protected override void OnResume()
  {
       SupportActionBar.SetTitle(Resource.String.app_name);
       base.OnResume();
  }

------------- HomeFragment.cs

 class HomeFragment: Fragment
    {
        public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);           
        }

     public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle     savedInstanceState)
        {
          View view = inflater.Inflate(Resource.Layout.homeLayout, container, false);
          return view;
       }
    }

---------- homeLayout.axml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="#ffffff"
        android:orientation="vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#ff46a2fd"
            android:text="Home"
            android:textSize="22dp"
            android:textStyle="bold"
            android:typeface="sans"
            android:gravity="center"
            android:layout_gravity="center"
            android:layout_centerInParent="true" />
        <Button
            android:id="@+id/btnProducts"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="3dp"
            android:layout_marginTop="3dp"
            android:background="#307FC1"
            android:text="merchant"
            android:layout_alignParentBottom="true"
            android:textColor="#ffffff" />
    </LinearLayout>
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

在OnCreateView方法的HomeFragment.cs中添加以下内容(在视图声明和返回参数之间):

var btnProducts = view.FindViewById<Button>(Resource.Id.btnProducts);
btnProducts.Click += btnProducts_Click;

然后为您的点击事件添加方法(在HomeFragment类的某个位置):

public void btnProducts_Click(object sender, EventArgs e)
{
    // Action you want to take upon button click
}