2017-04-06 3 views
0

Ich bin neu in NavigationDrawer, der folgende Code funktioniert.So erhalten Sie die Schaltfläche in FrameLayout für NavigationDrawer

Aber wie bekomme ich den Knopf in der homeLayout.axml, die wie in HomeFragment.cs nach dem untenstehenden Code angezeigt aufgeblasen wird.

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

1) Ich brauche Eventhandler zu diesem BTN hinzuzufügen: btnProducts in homeLayout.axml
- wo unter Code hinzufügen
, was ich Setup-Code hinzufügen müssen die btnProducts zu erhalten und fügen Sie Ereignis für diese Taste??

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

--- UI:

Haupt Layout-

<?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> 
Datei - Code für Haupt-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> 

Antwort

0

in Ihrem HomeFragment.cs im OnCreateView Methode fügen Sie die folgende (zwischen der Erklärung der Ansicht und der Rückkehr statment):

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

dann die Methode für Ihr Click-Ereignis hinzufügen (irgendwo in Ihrer HomeFragment Klasse):

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