[英]How can I programmatically remove a Navigation Drawer
我先要讲清楚-我不想关闭/隐藏抽屉。 那是微不足道的。 我想将其完全删除。
我继承了一个项目,其中使用了“调试抽屉”-一个在调试版本中显示但在生产应用程序中不显示的抽屉。 布局如下。 将ScrollView设置为visibility="gone"
无效。
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout_root"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout android:id="@+id/activity_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView android:id="@+id/debug_drawer"
android:visibility="gone"
android:layout_width="290dp"
android:layout_height="match_parent"
android:layout_gravity="end" />
<fragment android:id="@+id/navigation_drawer"
android:name="com.app.NavigationFragment"
android:layout_width="290dp"
android:layout_height="match_parent"
android:layout_gravity="start" />
</android.support.v4.widget.DrawerLayout>
我尝试了以下但没有成功
@Override
public void setContentView(int layoutResID) {
super.setContentView(R.layout.activity_layout);
if (BuildConfig.DEBUG) {
final ViewGroup debugDrawerContainer = findById(this, R.id.debug_drawer);
debugDrawerContainer.setVisibility(View.VISIBLE);
}
}
我已经找到一种执行此操作的方法,如下所示。
虽然这是一个技巧,但如果可以的话,一种更好的方法是创建另一个带有变量的构建变体以禁用抽屉(例如,告诉Android不在调试时不要首先创建抽屉,因为由blackbelt建议)
final ViewGroup debugDrawerContainer = findById(this, R.id.debug_drawer);
if (BuildConfig.DEBUG) {
...
} else {
((ViewGroup) debugDrawerContainer.getParent()).removeView(debugDrawerContainer);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.