android - Inheritance in Xamarin Android
问题描述
I use Xamarin for Android. I have two activities with some content, there is android.support.v4.widget.DrawerLayout
and android.support.design.widget.NavigationView
in XML files.
In MainActivity I use two object for content control. These are DrawerLayout
and NavigationView
. To switch between activities I use own method.
private void OnNavigationItemSelected(object sender, NavigationView.NavigationItemSelectedEventArgs e)
{
switch (e.MenuItem.ItemId)
{
case (Resource.Id.home):
//Go home
break;
case (Resource.Id.settings):
//Go to other activity
break;
}
drawerLayout.CloseDrawers();
}
DrawerLayout
, NavigationView
and OnNavigationItemSelected
is located in MainActivity, but I've created one more activities. This is Settings
and when I chose settings item, I'm moving to Settings
activity.
I wrote the same implementation in Settings
activity. I have DrawerLayout
, NavigationView
and OnNavigationItemSelected
.
It turns out that I just copied code. Maybe I should use inheritance? I just don't understand how can I realize that in Android application.
解决方案
为每个活动之间的共享创建通用 layout.xml。
放入一个我们称之为 xml 的文件
DrawerLayout
中。NavigationView
A.xml
包含
A.xml
在主页和设置的 xml 中。
例子
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include
layout="@layout/A"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
推荐阅读
- json - 为什么我在 Spring MVC 中得到 415 Unsupported Media Type?
- racket - DrRacket 帮助编写从 RES 类型中提取 RegE 的函数
- web-services - Flutter HTTP 请求使用 Basic Auth + 传递用户和密码来接收用户数据
- php - 如何修复 Drupal 更新:7082 - 将“jquery-extend-3.4.0.js”添加到“jquery”库
- git - ubuntu,如何使 .git 文件从终端可用
- spring - Spring Data JDBC - OneToMany 关系的不可变所有者
- scala - 无法在 Spark Scala 中将 RDD [Java 类] 转换为 Dataframe
- swift - 添加左键和标题后右键不显示
- javascript - 使用 jQuery Ajax 和 PHP 上传裁剪的图像
- java - 如何验证成员是否为管理员?