java - 其他活动中导航抽屉中工具栏下方的活动
问题描述
我在我的项目中默认添加导航抽屉活动,并通过扩展使项目单击转到新活动,但是当我在扩展活动上添加一些文本或按钮时,它会在工具栏下。那么我该如何解决这个问题。
这是我可以扩展其他新活动的导航活动
public class NavigationActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
Toolbar toolbar;
DrawerLayout drawer;
NavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
drawer = findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.navigation, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
Intent intent= new Intent(getApplicationContext(), LevelActivity.class);
switch (item.getItemId()){
case R.id.nav_home:
intent = new Intent(getApplicationContext(), HomeActivity.class);
startActivity(intent);
break;
case R.id.nav_sql:
startActivity(intent);
break;
case R.id.nav_c:
startActivity(intent);
break;
case R.id.nav_cpp:
startActivity(intent);
break;
}
drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
这是我要设计的新活动的 Xml 文件
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LevelActivity"
>
<TextView
android:text="This is problem"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
这是扩展类
public class LevelActivity extends NavigationActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//inflate your activity layout here!
View contentView = inflater.inflate(R.layout.activity_level, null, false);
drawer.addView(contentView, 0);
}
}
解决方案
推荐阅读
- c# - C# 数据协定类 - 扩展/添加属性
- java - 在多用户聊天 Android 客户端中使用 AsyncTask 是否比扩展连接线程更好?
- javascript - 创建霰弹枪传播模式 (babylon.js)
- java - springboot 自动关机并自动重启。
- angularjs - 如何在angularjs中创建无限滚动
- opencv - 如何使用轮廓来检测具有 1 个不均匀边的矩形?
- aws-api-gateway - 由于内部错误,AWS Cognito 授权方错误 500 执行失败 - JWT 声明中的值为空
- c# - 通过配置更改禁用实体框架预生成的视图?
- google-drive-api - 如何在 Google Drive REST API 中查看文件的权限?
- amazon-web-services - 亚马逊 AWS IPv6 Http 请求 --> EAFNOSUPPORT