我正在使用带有各种其他布局的LinearLayout
作为Xamarin(Android开发atm)中两个AXML文件底部的菜单栏。
在我的主要活动中,我设置了点击事件以在两个AXML视图之间切换,这两个视图都包含LinearLayout
菜单栏。
第一个按钮点击工作,并切换布局,但在此之后,我的点击事件不再起作用。我尝试在点击事件中重新初始化点击事件,但无济于事。
请考虑以下代码:
Main.AXML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/MyButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Main Page" />
<include
layout="@layout/menu_bar" />
</LinearLayout>
Other.AXML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/MyButton2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Other Page" />
<include
layout="@layout/menu_bar" />
</LinearLayout>
MainActivity.cs
using Android.App;
using Android.Widget;
using Android.OS;
namespace Tracker
{
[Activity(Label = "Tracker", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
LinearLayout newsMenuItemLayout = FindViewById<LinearLayout>(Resource.Id.newsMenuItem);
LinearLayout friendsMenuItemLayout = FindViewById<LinearLayout>(Resource.Id.friendsMenuItem);
newsMenuItemLayout.Click += (sender, args) =>
{
SetContentView(Resource.Layout.Other);
};
friendsMenuItemLayout.Click += (sender, args) =>
{
SetContentView(Resource.Layout.Main);
};
}
}
menu_bar.AXML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout03"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="75dp"
android:weightSum="100"
android:layout_alignParentBottom="true">
<LinearLayout
android:orientation="vertical"
android:layout_weight="20"
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="@+id/newsMenuItem"
android:background="@drawable/selector"
android:paddingTop="6dp"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true">
<ImageView
android:src="@drawable/news_icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/imageView1"
android:scaleType="fitCenter"
android:adjustViewBounds="false" />
<TextView
android:text="News"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView1"
android:gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_weight="20"
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="@+id/eventsMenuItem"
android:background="@drawable/selector"
android:paddingTop="6dp"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true">
<ImageView
android:src="@drawable/events_icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/imageView2"
android:scaleType="fitCenter"
android:adjustViewBounds="false" />
<TextView
android:text="Events"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView2"
android:gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_weight="20"
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="@+id/resultsMenuItem"
android:background="@drawable/selector"
android:paddingTop="6dp"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true">
<ImageView
android:src="@drawable/results_icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/imageView3"
android:scaleType="fitCenter"
android:adjustViewBounds="false" />
<TextView
android:text="Results"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView3"
android:gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_weight="20"
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="@+id/friendsMenuItem"
android:background="@drawable/selector"
android:paddingTop="6dp"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true">
<ImageView
android:src="@drawable/friends_icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/imageView4"
android:scaleType="fitCenter"
android:adjustViewBounds="false" />
<TextView
android:text="Friends"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView4"
android:gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_weight="20"
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="@+id/moreMenuItem"
android:background="@drawable/selector"
android:paddingTop="6dp"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true">
<ImageView
android:src="@drawable/more_icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/imageView5"
android:scaleType="fitCenter"
android:adjustViewBounds="false" />
<TextView
android:text="More"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView5"
android:gravity="center_horizontal" />
</LinearLayout>
我认为当我重新加载menu_bar时,我需要重新初始化我的点击事件,但是最好的方法是什么呢?
我之前将菜单栏加载为ActionBar
,我相信它是内容视图的独立实体,但由于这不可能从顶部移动,我不得不重新考虑这种方法。
非常感谢任何关于如何解决这个问题的想法/解释。
答案 0 :(得分:2)
我不完全确定为什么会出现这种情况,但解决办法可能是在主要布局中包含一个隐藏的布局。在您使用SetContentView的地方切换可见性。
<强> main.xml中强>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include
android:id="@+id/FirstLayout"
layout="@layout/first" />
<include
android:id="@+id/SecondLayout"
android:visibility="gone"
layout="@layout/second" />
<include
layout="@layout/menu_bar" />
</LinearLayout>
<强> first.axml 强>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/MyButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Main Page" />
</LinearLayout>
<强> second.axml 强>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/MyButton2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Main Page" />
</LinearLayout>
<强>活动强>
using Android.App;
using Android.Widget;
using Android.OS;
namespace Tracker
{
[Activity(Label = "Tracker", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
LinearLayout newsMenuItemLayout = FindViewById<LinearLayout>(Resource.Id.newsMenuItem);
LinearLayout friendsMenuItemLayout = FindViewById<LinearLayout>(Resource.Id.friendsMenuItem);
LinearLayout firstlayout = FindViewById<LinearLayout>(Resource.Id.FirstLayout);
LinearLayout secondlayout = FindViewById<LinearLayout>(Resource.Id.SecondLayout);
newsMenuItemLayout.Click += (sender, args) =>
{
firstlayout.Visibility = ViewStates.Visible;
secondlayout.Visibility = ViewStates.Gone;
};
friendsMenuItemLayout.Click += (sender, args) =>
{
firstlayout.Visibility = ViewStates.Gone;
secondlayout.Visibility = ViewStates.Visible;
};
}
}
}