2017-04-06 11 views
0

NavigationDrawerを初めて使用しました。以下のコードは動作しています。NavigationDrawerのFrameLayoutのボタンを取得するには

しかし、HomeLayout.axml内のボタンを取得するには、次のコードの後に​​HomeFragment.csに示されているように膨張させます。

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

1)私はこのBTNになeventHandlerを追加する必要があります。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; 
     } 
    } 
OnCreateView方法であなたのHomeFragment.csで

---------- 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> 

答えて

0

は、(ビューの宣言とリターンなステートメントの間)次の行を追加します。

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 
} 
関連する問題