Java android中的导航抽屉不是全屏的
由于谷歌推出了导航抽屉,我尝试使用这个组件创建一个类似facebook的菜单。问题是,视觉效果似乎不同 当抽屉打开时,google one会保留操作栏,而facebook one不会。相反,整个屏幕都被推到了右侧 我发现有一些lib可以实现这一点,但由于我不喜欢在项目中包含第三方lib,有什么方法可以实现这一点吗?谢谢 基于导航抽屉的代码教程Java android中的导航抽屉不是全屏的,java,android,android-actionbar,sidebar,navigation-drawer,Java,Android,Android Actionbar,Sidebar,Navigation Drawer,由于谷歌推出了导航抽屉,我尝试使用这个组件创建一个类似facebook的菜单。问题是,视觉效果似乎不同 当抽屉打开时,google one会保留操作栏,而facebook one不会。相反,整个屏幕都被推到了右侧 我发现有一些lib可以实现这一点,但由于我不喜欢在项目中包含第三方lib,有什么方法可以实现这一点吗?谢谢 基于导航抽屉的代码教程 protected void setupMenu(List<String> list, final ListView menu) {
protected void setupMenu(List<String> list, final ListView menu) {
Adapter customAdapter = new Adapter(getActionBar().getThemedContext(),
R.layout.item, list);
menu.setAdapter(customAdapter);
menu.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
final int pos, long id) {
String selected = ((TextView) view.findViewById(R.id.itemTxt))
.getText().toString();
// define pass data
final Bundle bData = new Bundle();
bData.putString("itemSelect", selected);
drawer.setDrawerListener(new DrawerLayout.SimpleDrawerListener() {
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
FragmentTransaction tx = getSupportFragmentManager()
.beginTransaction();
tx.replace(R.id.mainContent, Fragment.instantiate(
MainActivity.this,
"com.example.utilities.ContentPage", bData));
tx.commit();
}
});
drawer.closeDrawer(menu);
}
});
}
protectedvoid设置菜单(列表、最终列表视图菜单){
Adapter customAdapter=新适配器(getActionBar().getThemedContext(),
R.布局、项目、列表);
menu.setAdapter(customAdapter);
menu.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父级、视图、,
最终整数位置,长id){
所选字符串=((TextView)view.findViewById(R.id.itemTxt))
.getText().toString();
//定义通行证数据
最终Bundle bData=新Bundle();
bData.putString(“itemSelect”,选中);
drawer.setDrawerListener(新的DrawerLayout.SimpleDrawerListener(){
@凌驾
图纸上的公共空白已关闭(视图抽屉视图){
super.onDrawerClosed(抽屉视图);
FragmentTransaction tx=getSupportFragmentManager()
.beginTransaction();
tx.replace(R.id.mainContent,Fragment.instantiate(
这个,,
“com.example.utilities.ContentPage”,bData);
tx.commit();
}
});
抽屉。关闭抽屉(菜单);
}
});
}
创建自定义导航抽屉是您的最佳解决方案。
我知道您不想使用第三方,但这可以快速解决滑动菜单库的问题。以下是一个对我有效的快速解决方案:
<include
android:id="@+id/left_drawer"
android:orientation="vertical"
**android:layout_width="320dp"**
android:layout_height="match_parent"
android:layout_gravity="start"
layout="@layout/drawer"/>
设置包含布局的宽度。对于具有不同屏幕大小的设备,您可以动态设置此包含布局的宽度
祝你一切顺利 如果您查看DroperLayout的源代码,您将看到,这个最小边距的resposnible是变量mmindravermargin 因此,至少有两种解决方案(技巧) 1.扩展抽屉布局并使用反射将此变量设置为0。 从所有构造函数调用此方法
private void init() {
try {
Field declaredField = getClass().getSuperclass().getDeclaredField("mMinDrawerMargin");
declaredField.setAccessible(true);
declaredField.setInt(declaredField, 0);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// taken from parents logic
float density = this.getResources().getDisplayMetrics().density;
int minMargin = (int) (64.0F * density + 0.5F);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int newWidth = MeasureSpec.makeMeasureSpec(widthSize + minMargin, widthMode);
super.onMeasure(newWidth, heightMeasureSpec);
}
我还在此处创建了示例项目此问题是由边距引起的,因此我们必须重置宽度: 我通过实现DroperListener并将ListView包装到一个LinearLayout中来解决这个问题,以便为列表视图之外的其他视图腾出空间 这是我的听众
public class NavigationDrawer implements DrawerLayout.DrawerListener {
private DrawerLayout mDrawerLayout;
protected boolean expanded = false;
NavigationDrawer(Activity activity) {
this.activity = activity;
}
public NavigationDrawer bindDrawerLayout(int id) {
mDrawerLayout = (DrawerLayout) activity.findViewById(id);
mDrawerLayout.setDrawerListener(this);
return this;
}
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
if (!expanded) {
Log.i("margin", "here we are");
LinearLayout layout = (LinearLayout) findViewById(R.id.left_drawer);
layout.getLayoutParams().width = getResources().getDisplayMetrics().widthPixels;
layout.requestLayout();
expanded = true;
}
}
@Override
public void onDrawerOpened(View drawerView) {
}
@Override
public void onDrawerClosed(View drawerView) {
}
@Override
public void onDrawerStateChanged(int newState) {
}
}
以下是我的布局:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/application_drawer_layout"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#ffffff">
<FrameLayout
android:id="@+id/application_content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:id="@+id/left_drawer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="start"
android:background="#000000"
android:orientation="vertical">
<ListView
android:id="@+id/application_left_drawer"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
如果您想实现类似facebook的滑动菜单,那么您需要使用androids而不是NavigationDrawer
<android.support.v4.widget.SlidingPaneLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout android:layout_width="250dp"
android:layout_height="match_parent"
android:background="#CC00FF00" />
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="match_parent"
android:background="#CC0000FF" >
// add toolbar and other required layouts
</LinearLayout>
</android.support.v4.widget.SlidingPaneLayout>
//添加工具栏和其他必需的布局
添加工具栏而不是actionbar,并且不应用actionbar主题。希望这有帮助
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
>
<include
layout="@layout/nav_header_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</android.support.design.widget.NavigationView>
第一个方法抛出非法参数异常,第二个方法无效:(请提供stacktrace。您是否尝试从我提供的bitbucket运行项目?
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer"