android 抽屉宽度,Android设置导航抽屉列表,打开所有设备屏幕的确切一半的屏幕...

动态设置ListView的宽度…

mDrawerLayout = (DrawerLayout) view.findViewById(R.id.drawer_layout); mDrawerList = (ListView) view.findViewById(R.id.top_sectionlist); int width = getResources().getDisplayMetrics().widthPixels/2; DrawerLayout.LayoutParams params = (android.support.v4.widget.DrawerLayout.LayoutParams) mDrawerList.getLayoutParams(); params.width = width; mDrawerList.setLayoutParams(params);

您必须手动计算屏幕大小并在运行时将动态宽度设置为抽屉布局,以下是如何在运行时获取设备屏幕宽度和高度

使用此代码,您可以获得运行时显示的宽度和高度

DisplayMetrics displaymetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); int height = displaymetrics.heightPixels; int width = displaymetrics.widthPixels;

现在,您需要将宽度除以2

int newWidth=width/2;

现在将宽度设置为ListView,就像这样

drawer_list.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,newWidth));

这适用于所有设备,并且将在所有设备上提供统一的大小。

这是我的解决方案,其他人需要几个百分比来显示导航抽屉。

在这里,我使用右侧导航抽屉,当抽屉向左移动时,我显示15%的主屏幕。

// convert your offset percent (here 15% ) into decimal by dividing 100 float offset = .15f * getResources().getDisplayMetrics().widthPixels ; float width = getResources().getDisplayMetrics().widthPixels - offset; DrawerLayout.LayoutParams params = (android.support.v4.widget.DrawerLayout.LayoutParams) rightNavigationView.getLayoutParams(); params.width = (int) width; rightNavigationView.setLayoutParams(params);

使用新的Android支持设计库,您可以使用android.support.percent.PercentRelativeLayout轻松完成此操作。 这是我的解决方案。

如果你问为什么在DrawerLayout和内容中有一个android:layout_marginRight="-64dp"和android:layout_marginRight="-64dp" 。 这是因为64 dp的固定边距直接编码到DrawerLayout ,不允许你使用抽屉的全部宽度。 更多关于这一点

int currentVerion = Build.VERSION.SDK_INT; int width= 0; Display display = getWindowManager().getDefaultDisplay(); Point size = new Point(); if(currentVerion >= Build.VERSION_CODES.BASE) { display.getSize(size); width = size.x; } else { width = display.getWidth(); } ((ListView) view.findViewById(R.id.top_sectionlist)).getLayoutParams().width =width/2;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值