Common Header with click events of Header from all Activities in Xamarin android

时光怂恿深爱的人放手 提交于 2020-01-06 18:26:07

问题


I am developing a xamarin android application, where I used to call a Header activity in all Activities. My code is as Fallows

My Main.axml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<!-- Header aligned to top -->
<include layout="@layout/Header"
       android:id="@+id/includeheader"
        android:layout_alignParentTop="true" />

<!-- Content below header and above footer -->
<include layout="@layout/Content"
       android:id="@+id/includecontent" />

<!-- Footer aligned to bottom -->
<include layout="@layout/Footer"
       android:id="@+id/includefooter"
       android:layout_alignParentBottom="true"/>

</RelativeLayout>

My Header.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TableLayout
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:minHeight="50dip">
<TableRow
    android:background="#2c2c2c"
    android:padding="10dip">
  <TextView
      android:id="@+id/scanHome"
      android:layout_width="0dip"
      android:layout_weight="2.5"
      android:textSize="22sp"
      android:textStyle="bold"
      android:gravity="center"
      android:text="" />
  <Button
      android:id="@+id/Settings"
      android:layout_width="0dip"
      android:layout_height="30dip"
      android:layout_marginLeft="10dip"
      android:layout_weight="0.17"
      android:gravity="center"
      android:width="35dip" 
      android:clickable="true"
      android:onClick="SettingsClick"/>
  <Button
      android:id="@+id/logout"
      android:layout_width="0dip"
      android:layout_height="40dip"
      android:layout_weight="0.27"
      android:gravity="center"
      android:width="40dip" />
</TableRow>
</TableLayout>

</LinearLayout>

MainActivity.class

namespace LayoutApp
{
[Activity(Label = "LayoutApp", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Header 
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);
        setHeading("Scan Home");
    }
}
}

Header.class

[Activity(Label = "LayoutApp", MainLauncher = false)]
public abstract class Header : Activity , View.IOnClickListener
{
    private TextView HeaderText;
    private Button Settings;

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

        SetContentView(Resource.Layout.Header);

        Settings = FindViewById<Button>(Resource.Id.Settings);

        Settings.Click += delegate
        {

        };
    }

    protected void setHeading(string text)
    {
        if (HeaderText == null)
            HeaderText = FindViewById<TextView>(Resource.Id.scanHome);
        if (HeaderText != null)
            HeaderText.Text = text;
    }

    public void SettingsClick()
    {

    }
}

Hence I am using Header Activity in MainActivity like in native android using include Property. When I load Main Launcher, Header is also displaying but click events are not working from the MainActivity where as text is applying from setHeading method.

When debugging , error is populating as 'java.lang.illegalistateexception: could not find a method SettingsClick(View) in the activityclass for onclick handler on view class'.

So, my issue here is I would like to get click events of Header.


回答1:


The layout files (AXML) are not linked to Activities with the same names as theirs.

In your Main.axml code, you are including (adding) the layout code from Header.axml; however, your MainActivity.cs code has no relations with the Main.axml, just like the HeaderActivity.cs code has no relations with the Header.axml code.

In the MainActivity.cs code, the SetContentView(Resource.Layout.Main) applies the Main.axml layout (which contains the Header.axml code) to your MainActivity, but does not apply the methods from HeaderActivity.cs.

If you want an app with Toolbar and Bottom Bar in your app, there is a good tutorial here: http://mateoj.com/2015/06/21/adding-toolbar-and-navigation-drawer-all-activities-android/

You should also take a look on Android Fragments.




回答2:


@kumar Sudheer, I don't know the xamarin development, but I can understand what you want to do by looking at the code and exception you get.

Just pass the View object as parameter in the SettingClick method of your header activity

public void SettingsClick(View v)
{

}

In android when you define the click handler in layout file then the signature of that method would be public void <clickHandler>(View v) {}.

You've not passed the View parameter in the method that's why system is unable to find your method in Activity and that's why you are getting the java.lang.IllegaliStateException



来源:https://stackoverflow.com/questions/43847378/common-header-with-click-events-of-header-from-all-activities-in-xamarin-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!