c# - Xamarin.Android:切换视图
问题描述
我有由 Visual Studio 创建的默认生成的导航抽屉模板。但是如何切换视图?我不想使用新的活动,因为我希望导航保持不变。您知道如何更改片段或其他内容吗?我有 content_main.xml 和这应该改变,但我不知道如何
请查看我的Github 存储库以供参考
解决方案
你可以试试下面的代码。
layout_SwitchView.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/FirstLayout"
layout="@layout/layout_sw_first"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<include
android:id="@+id/SecondLayout"
layout="@layout/layout_sw_second"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<include
android:id="@+id/menu_bar"
layout="@layout/layout_sw_menubar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
layout_SW_first.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="First Layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
布局_SW_second.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
layout_SW_menubar.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<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/FirstMenuItem"
android:paddingTop="6dp"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true">
<ImageView
android:src="@drawable/tab_graph"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/imageView1"
android:scaleType="fitCenter"
android:adjustViewBounds="false" />
<TextView
android:text="First"
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/SecondMenuItem"
android:paddingTop="6dp"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true">
<ImageView
android:src="@drawable/tab_chat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/imageView2"
android:scaleType="fitCenter"
android:adjustViewBounds="false" />
<TextView
android:text="Second"
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/moreMenuItem"
android:paddingTop="6dp"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true">
<ImageView
android:src="@drawable/star_small"
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>
</LinearLayout>
</RelativeLayout>
Activity_SwitchView.cs:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
SetContentView(Resource.Layout.layout_SwitchView);
LinearLayout FirstMenuItemLayout = FindViewById<LinearLayout>(Resource.Id.FirstMenuItem);
LinearLayout SecondMenuItemLayout = FindViewById<LinearLayout>(Resource.Id.SecondMenuItem);
LinearLayout firstlayout = FindViewById<LinearLayout>(Resource.Id.FirstLayout);
LinearLayout secondlayout = FindViewById<LinearLayout>(Resource.Id.SecondLayout);
FirstMenuItemLayout.Click += (sender, args) =>
{
firstlayout.Visibility = ViewStates.Visible;
secondlayout.Visibility = ViewStates.Gone;
};
SecondMenuItemLayout.Click += (sender, args) =>
{
firstlayout.Visibility = ViewStates.Gone;
secondlayout.Visibility = ViewStates.Visible;
};
}
截屏:
推荐阅读
- javascript - 如何用烧瓶和 jquery 解决这个问题?HTTP400: BAD REQUEST - 由于语法无效,服务器无法处理请求
- c# - error CS0150: A constant value is expected //switch 语句中的错误
- sql - 如何根据最大列值找到对应的行数据?
- python - 如何在 Python 中将列表 [a,b] 转换为 [[a],[b]]?单行功能,例如地图
- javascript - html2canvas 导致 IE11 挂起并进入“无响应”状态
- hibernate - 给定其类或类型,是否有任何方法可以创建持久实体示例?
- complexity-theory - 随机填充的四叉树中的平均搜索复杂度
- javascript - 在呈现之前更改状态
- flutter-test - 如何在 Flutter Driver 中测试 ImagePicker?
- python - 通过 Beautifulsoup 获取 span 的内容属性