我已经使用了Android Studio中提供的Navigation Drawer模板。
在我的 Activity 布局中,我有以下代码:
<fragment android:id="@+id/navigation_drawer"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:name="com.smedilepaolo.newsfeed.NavigationDrawer.NavigationDrawerFragment"
tools:layout="@layout/fragment_navigation_drawer" />
我需要以编程方式更改面板的宽度。
我认为更改它的正确区域是:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news_feed);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
....
但是我不明白如何实现我的目标。
/ *更新* /
抽屉面板的图标未显示在操作栏中
mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
mDrawerListView.post(new Runnable() {
@Override
public void run() {
Resources resources = getResources();
float width = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 240, resources.getDisplayMetrics());
DrawerLayout.LayoutParams params = (DrawerLayout.LayoutParams) mDrawerListView.getLayoutParams();
params.width = (int) (width);
mDrawerListView.setLayoutParams(params);
}
});
selectItem(position);
}
});
mDrawerListView.post(new Runnable() {
@Override
public void run() {
Resources resources = getResources();
float width = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, resources.getDisplayMetrics().widthPixels-1, resources.getDisplayMetrics());
DrawerLayout.LayoutParams params = (DrawerLayout.LayoutParams) mDrawerListView.getLayoutParams();
params.width = (int) (width);
mDrawerListView.setLayoutParams(params);
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
mDrawerLayout.openDrawer(mFragmentContainerView);
}
});
那就是selectItem方法
private void selectItem(int position) {
mCurrentSelectedPosition = position;
if (mDrawerListView != null) {
mDrawerListView.setItemChecked(position, true);
}
if (mDrawerLayout != null) {
mDrawerLayout.closeDrawer(mFragmentContainerView);
}
....
最佳答案
根据从Navigation Drawer模板生成的代码,可以通过将代码添加到NavigationDrawerFragment中的onCreateView()方法来更改面板的宽度。
mDrawerListView.post(new Runnable() {
@Override
public void run() {
Resources resources = getResources();
float width = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, resources.getDisplayMetrics());
DrawerLayout.LayoutParams params = (DrawerLayout.LayoutParams) mDrawerListView.getLayoutParams();
params.width = (int) (width);
mDrawerListView.setLayoutParams(params);
}
});
要在DrawerLayout上禁用平移手势:
//NavigationDrawerFragment
public void setUp(int fragmentId, DrawerLayout drawerLayout) {
mDrawerLayout = drawerLayout;
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}
要打开/关闭DrawerLayout:
mDrawerLayout.openDrawer(mDrawerListView);
mDrawerLayout.closeDrawer(mDrawerListView);
为防止抽屉面板的图标消失,请按如下所示更改setOnItemClickListener方法:
mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
});
并修改覆盖的onDrawerClose方法,如下所示:
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
if (!isAdded()) {
return;
}
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
Resources resources = getResources();
float width = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 240, resources.getDisplayMetrics());
DrawerLayout.LayoutParams params = (DrawerLayout.LayoutParams) mDrawerListView.getLayoutParams();
params.width = (int) (width);
mDrawerListView.setLayoutParams(params);
getActivity().invalidateOptionsMenu(); // calls onPrepareOptionsMenu()
}