gpt4 book ai didi

java - Android以编程方式更改抽屉导航面板的宽度

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:58:15 25 4
gpt4 key购买 nike

我使用了 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()
}

关于java - Android以编程方式更改抽屉导航面板的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25587656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com