Java android中的导航抽屉不是全屏的
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20190201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Navigation drawer in android is not full screen
提问by user782104
Since the google has introduced the navigation drawer, I tried to use this component to create a facebook-like menu. The problem is , the visual effect is seems to be different.
由于google引入了导航抽屉,我尝试使用这个组件来创建一个类似facebook的菜单。问题是,视觉效果似乎不同。
The google one has the action bar retain when the drawer is open while the facebook one does not.Instead, the whole screen has pushed to right side
当抽屉打开时,google 的操作栏会保留,而 facebook 的则不会。相反,整个屏幕都推到了右侧
I have found there are some lib can achieve this, but since I prefer not include third party lib in the project, are there any way to achieve this ? Thanks
我发现有一些库可以实现这一点,但由于我不想在项目中包含第三方库,有没有办法实现这一点?谢谢
Code based on navigation drawer tutorial
基于导航抽屉的代码教程
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);
}
});
}
采纳答案by aNoviceGuy
回答by GOLDEE
Here is a quick solution that worked for me :
这是一个对我有用的快速解决方案:
<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"/>
Set width of included layout . For devices with different screen size you can dynamically set the width of this included layout .
设置包含布局的宽度。对于具有不同屏幕尺寸的设备,您可以动态设置此包含布局的宽度。
All the best !!!!
祝一切顺利 !!!!
回答by wnc_21
If you will check source code of DrawerLayout, you will see, that resposnible for this minimum margin is the variable mMinDrawerMargin
如果你检查 DrawerLayout 的源代码,你会看到,这个最小边距是变量 mMinDrawerMargin
So, there are atleast 2 solutions(tricks) 1. extend DrawerLayout and set this variable to 0 with reflection. call this method from all constructors.
因此,至少有 2 个解决方案(技巧) 1. 扩展 DrawerLayout 并通过反射将此变量设置为 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();
}
}
- not so tricky
- 没那么棘手
overryde onMeasure method like this
像这样覆盖 onMeasure 方法
@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);
}
I also created sample project here https://bitbucket.org/wnc_21/fsnavigationdrawer
我还在此处创建了示例项目https://bitbucket.org/wnc_21/fsnavigationdrawer
回答by Hassan Gilak
this problem is caused by margin so we have to reset the width : i solved this problem by implementing DrawerListener and wrapping ListView inside a LinearLayout for making rooms for other views beside list view
这个问题是由边距引起的,所以我们必须重置宽度:我通过实现 DrawerListener 并将 ListView 包装在 LinearLayout 中为列表视图旁边的其他视图腾出空间来解决这个问题
here is my listener
这是我的听众
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) {
}
}
here is my layout :
这是我的布局:
<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>
回答by Suresh Kumar
If you want to implement like facebook sliding menu, then you to use androids SlidingPaneLayoutinstead of NavigationDrawer.
如果你想实现像 facebook 滑动菜单一样,那么你可以使用androids SlidingPaneLayout而不是 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>
add toolbar instead of actionbar and apply no actionbar theme.
添加工具栏而不是操作栏并且不应用任何操作栏主题。
回答by mughil
Hope this Helps.
希望这可以帮助。
<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>
Remove the last two lines in the default code
去掉默认代码的最后两行
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer"